- Home /
Question by
BodaciousP · Dec 04, 2018 at 06:36 PM ·
c#vehicleracing game
[C#] Vehicle Controller Acting Very Jittery?
I'm in the process of creating an anti-gravity racing game, similar to F-Zero. I have my vehicle movement script mostly working, but when I have my vehicle go up curved roads it behaves very jittery. Is there anything any of you can see in my script that would be causing this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VehicleControl : MonoBehaviour {
public GameObject pointA;
public bool canMove;
public LayerMask layerMask;
float currentBody;
public float body;
public float accelleration;
public float speed;
public float weight;
public float handling;
public float accellTime;
public float drift;
float timeHold;
Rigidbody rb;
Vector3 trueDir;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
timeHold = 0.141f;
}
// Update is called once per frame
void Update () {
if (Input.GetButton("Move") && timeHold < accellTime) {
timeHold += Time.deltaTime;
} else if (timeHold > 0.141f) {
timeHold -= Time.deltaTime * 4;
}
if (Input.GetButton("StrafeL")) {
transform.Rotate(transform.up, -drift * Time.deltaTime, Space.World);
trueDir = Quaternion.Euler(0, -drift / 2, 0) * transform.forward;
} else if (Input.GetButton("StrafeR")) {
transform.Rotate(transform.up, drift * Time.deltaTime, Space.World);
trueDir = Quaternion.Euler(0, drift / 2, 0) * transform.forward;
} else {
trueDir = transform.forward;
}
if (timeHold > 0.141f) {
rb.velocity = (speed * (((100 * (Mathf.Pow(timeHold, 2)) - 2) / (Mathf.Pow(timeHold, 2) + (accelleration * timeHold))))) * trueDir;
} else {
rb.velocity = 0 * transform.forward;
}
transform.Rotate(transform.up, handling * Input.GetAxis("Horizontal") * Time.deltaTime, Space.World);
}
void FixedUpdate() {
RaycastHit p1;
Physics.Raycast(pointA.transform.position, -pointA.transform.up, out p1, Mathf.Infinity, layerMask);
Debug.DrawRay(pointA.transform.position, -pointA.transform.up * 10, Color.red);
Quaternion rot = Quaternion.LookRotation(Vector3.ProjectOnPlane(transform.forward, p1.normal), p1.normal);
transform.rotation = rot;
if (p1.collider) {
if (p1.distance > 1.75f || p1.distance < 1.75f) {
transform.position = p1.point + (rot * new Vector3(0, 1.75f, 0));
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[C#] Vehicle Controller Acting Very Jittery? 0 Answers
Unity car won't follow old waypoint 0 Answers