This is a reference page for Unity Scripts
Car
Camera Offset
//this script is attached to the Main Camera, you will need to drag the Vehicle into the "public GameObject player" variable
​
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public GameObject player;
//create offset variable for camera
private Vector3 offset = new Vector3 (.1f, 6, -8);
void Update()
{
//gets the the players position plus the offset and assigns it to the cameras current
transform.position = player.transform.position + offset;
}
}
Controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public float turnSpeed;
public float horizontalInput;
public float forwardInput;
void Update()
{
//these are used to access the a,d keys -1 +1 and the w,s keys +1,-1 then assign them to a variable
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
//access the position component, move it forward (z axis) times the speed and either +1 or -1
​
transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
//access the rotation component, move it left or right (y axis) times the turnspeed and either +1 or -1
transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed * horizontalInput);
}
}
Rotate Wheels
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
public float speed = 10;
public float forwardInput;
void Update()
{
forwardInput = Input.GetAxis("Vertical");
transform.Rotate(Vector3.right * Time.deltaTime * speed * forwardInput);
}
}
Plane
Rotate Prop
Camera Offset
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateProp : MonoBehaviour
{
public float speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Rotate(Vector3.forward * Time.deltaTime * speed);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayerX : MonoBehaviour
{
public GameObject plane;
private Vector3 offset = new Vector3 (29,4,11);
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = plane.transform.position + offset;
}
}
Controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControllerX : MonoBehaviour
{
public float speed;
public float rotationSpeed;
public float verticalInput;
public float sidewaysInput;
public float rlspeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
// get the user's vertical input
verticalInput = Input.GetAxis("Vertical");
sidewaysInput = Input.GetAxis("Horizontal");
// move the plane forward at a constant rate
transform.Translate(Vector3.forward * Time.deltaTime * speed);
transform.Rotate(Vector3.up * Time.deltaTime * rlspeed * sidewaysInput);
// tilt the plane up/down based on up/down arrow keys
transform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime * -verticalInput);
}
}