- Home /
Wheels that just plain won't spin
Here's the whole code in case there's anything I missed that you might catch.
using UnityEngine;
using System.Collections;
public class CrimsonSisters : MonoBehaviour {
float thrust;
private Animator sistersAnim;
public GameObject[] wheels;
Vector3[] wheelsPos;
Vector3[] prevContact;
float[] wheelSpin;
float[] wheelVel;
private float steering;
private float sliding;
private float fall;
private float speed;
public float fallThreshold;
public float speedThreshold;
public float grip;
public GUIText kmh;
public GameObject dirt;
public Camera[] cameras;
int currentCam = 1;
WholeGame game;
void Start () {
wheelSpin = new float[4];
wheelVel = new float[4];
wheelsPos = new Vector3[4];
sistersAnim = this.GetComponent<Animator> ();
game = GameObject.Find ("Supercontroller").GetComponent<WholeGame> ();
for (int i = 0; i < 4; i++) wheelsPos[i] = wheels[i].transform.localPosition;
}
void OnCollisionStay (Collision other) {
for (int i = 0; i < 4; i++) {
if (other.contacts[0].thisCollider == wheels[i]) {
wheelSpin[i] = transform.InverseTransformDirection (rigidbody.velocity).z * 2;
if (other.gameObject.tag == "Dirt" && rigidbody.velocity.magnitude > 0.5f) Instantiate (dirt, other.contacts [0].point, transform.rotation);
float wheelCollision = (other.contacts [0].point - prevContact[i]).magnitude / speedThreshold;
wheels[i].transform.localPosition = new Vector3(wheelsPos[i].x, Mathf.Lerp (wheels[i].transform.localPosition.y, wheelsPos[i].y, wheelCollision), wheelsPos[i].z);
prevContact[i] = other.contacts [0].point;
}
}
}
void FixedUpdate () {
steering = Mathf.Lerp (steering, game.steering, Time.deltaTime * 15 / (rigidbody.velocity.magnitude / 10 + 1));
sliding = Mathf.Lerp (sliding, game.sliding, Time.deltaTime * 15);
fall = Mathf.Lerp (fall, rigidbody.velocity.y / fallThreshold, Time.deltaTime * 100);
speed = Mathf.Lerp (speed, rigidbody.velocity.magnitude / speedThreshold, Time.deltaTime * 20);
grip = Mathf.Lerp (0f, 1f, speed);
sistersAnim.SetFloat ("Steering", steering * 0.5f + 0.5f);
thrust = thrust * 0.95f;
thrust += game.accel * 2;
thrust -= Mathf.Abs (sliding) * 0.4f;
thrust = Mathf.Clamp (thrust, -10, 100);
for (int i = 0; i < 4; i++) {
wheels[i].transform.RotateAround (wheels[i].transform.position, wheels[i].transform.up, wheelSpin[i]);
wheelSpin[i] = wheelSpin[i] * 0.95f;
wheels[i].transform.localPosition = new Vector3(wheelsPos[i].x,
Mathf.SmoothDamp (wheels[i].transform.localPosition.y, wheelsPos[i].y-0.05f, ref wheelVel[i], 0.1f),
wheelsPos[i].z);
wheels[i].transform.localPosition = new Vector3(wheelsPos[i].x,
Mathf.Clamp(wheels[i].transform.localPosition.y, wheelsPos[i].y-0.05f, wheelsPos[i].y),
wheelsPos[i].z);
}
// The important part
wheels [0].transform.localRotation = Quaternion.Euler (wheels [0].transform.localRotation.eulerAngles.x,
wheels [0].transform.localRotation.eulerAngles.y,
Mathf.Lerp (-45, 45, steering * 0.5f + 0.5f));
wheels [1].transform.localRotation = Quaternion.Euler (wheels [1].transform.localRotation.eulerAngles.x,
wheels [1].transform.localRotation.eulerAngles.y,
Mathf.Lerp (-45, 45, steering * 0.5f + 0.5f));
wheels [2].transform.RotateAround (wheels [2].transform.position, wheels [2].transform.up, thrust);
wheels [3].transform.RotateAround (wheels [3].transform.position, wheels [3].transform.up, thrust);
rigidbody.AddForce (Vector3.down * 15);
rigidbody.angularVelocity = Vector3.zero;
kmh.text = (rigidbody.velocity.magnitude * 3).ToString ("F0") + " km/h";
dirt.transform.localScale = new Vector3 (rigidbody.velocity.magnitude,
rigidbody.velocity.magnitude,
rigidbody.velocity.magnitude) * 0.1f;
}
}
I mean for the back wheels to rotate and drive a car and the front wheels to rotate left and right 45 degrees so as to steer it. But no matter what, nothing rotates at all whether I'm using transform.RotateAround or directly setting the wheels' rotation. I made sure not to have the wheels use a skinned mesh renderer, the wheels are properly referenced in the inspector and adding a Debug.Log() line referencing thrust/steering reveals that they're not the problem. I do see, however, that the rotation doesn't move at all in the inspector at runtime.
Am I missing something obvious?
edit: I might as well add that the wheels are childed to the car, and that they do spin when de-childed. In which case they don't follow the car and collide with it, so it's something I really don't want to do.
game = GameObject.Find ("Supercontroller").GetComponent<WholeGame> ();
Can you explain what's happening there?
This function's syntax is pretty odd. The "supercontroller" is a controller I'm using that handles game-wide stuff across scenes (so far just input). This line gets it so the car gets values from it at runtime (input again). The supercontroller is in the scene and input functions correctly.
Your answer
Follow this Question
Related Questions
WheelCollider rotates mesh wrong 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make a functional steering wheel for a car game? 2 Answers
tachometer??? Help!! 1 Answer