So i am making a racing game, and i had the controls nice, but collisions didn’t work. I attempted to fix this, and my character no longer falls through the ground, but now i have even more problems. could somebody help me out on where i went wrong with my driving script? as in the title, when i run the game, pressing “W” makes the car go left, and “S” makes the car go right. and now for some reason it doesn’t turn at all. I also noticed that if you quickly alternate between “W” and “S”, you can make the car inch forward very slightly. I’m probably making a simple mistake, but i just can’t figure it out. here’s my driving script:
using UnityEngine;
using System.Collections;
public class ThirdControl : MonoBehaviour {
// the first capital letter in the variable names mean the key that is pressed (W and S), the second capital letter means direction (North, South, East, West)
private Vector3 DefaultRot = new Vector3 (270, 0, 0);
public Vector3 turnA = new Vector3(0, -10 ,0);
public Vector3 turnD = new Vector3(0, 10 ,0);
public Vector3 moveSN = new Vector3(0, 0 ,-10);
public Vector3 moveWN = new Vector3(0, 0 ,10);
public Vector3 moveSS = new Vector3(0, 0 ,10);
public Vector3 moveWS = new Vector3(0, 0 ,-10);
public Vector3 moveSE = new Vector3(-10, 0 ,0);
public Vector3 moveWE = new Vector3(10, 0 ,0);
public Vector3 moveSW = new Vector3(10, 0 ,0);
public Vector3 moveWW = new Vector3(-10, 0 ,0);
void FixedUpdate() {
//checks if the car is rotated within these angles
if (transform.rotation.eulerAngles.y >= 0 && transform.rotation.eulerAngles.y <= 45 || transform.rotation.eulerAngles.y >= 315 && transform.rotation.eulerAngles.y <= 360) {
//moves the car forward using physics, i can't use transform.Translate becuase i need collisions to happen
if (Input.GetKey ("w")) {
rigidbody.AddForce (moveWN);
} else if (Input.GetKey ("s")) {
rigidbody.AddForce (moveSN);
}
} else if (transform.rotation.eulerAngles.y >= 135 && transform.rotation.eulerAngles.y <= 180 || transform.rotation.eulerAngles.y >= 180 && transform.rotation.eulerAngles.y <= 225) {
if (Input.GetKey ("w")) {
rigidbody.AddForce (moveWS);
} else if (Input.GetKey ("s")) {
rigidbody.AddForce (moveSS);
}
} else if (transform.rotation.eulerAngles.y >= 45 && transform.rotation.eulerAngles.y <= 90 || transform.rotation.eulerAngles.y >= 90 && transform.rotation.eulerAngles.y <= 135) {
if (Input.GetKey ("w")) {
rigidbody.AddForce (moveWE);
} else if (Input.GetKey ("s")) {
rigidbody.AddForce (moveSE);
}
} else if (transform.rotation.eulerAngles.y >= 225 && transform.rotation.eulerAngles.y <= 270 || transform.rotation.eulerAngles.y >= 270 && transform.rotation.eulerAngles.y <= 315){
if (Input.GetKey ("w")) {
rigidbody.AddForce (moveWW);
} else if (Input.GetKey ("s")) {
rigidbody.AddForce (moveSW);
}
//if the car's rotation reaches 360, revert it back to zero
if (transform.rotation.eulerAngles.y > 360){
transform.eulerAngles = (DefaultRot);
}
//turns the car
if (Input.GetKey ("a")){
rigidbody.AddTorque(turnA);
}else
if (Input.GetKey ("d")){
rigidbody.AddTorque(turnD);
}
}}}