- Home /
Axle scrpt
I need some help because I'm trying to make this script for a truck axle I made but my problem is that I can't figure out how to get the axle to properly stay in the middle of the 2 wheels y axis and I also get a error saying that the rotation is nan. Thank You.
using UnityEngine;
using System.Collections;
public class TruckAxle : MonoBehaviour {
public Transform LeftWheel;
public Transform RightWheel;
public Transform Axle;
public float WheelDistance;
public float WheelY1;
public float WheelY2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
WheelY1 = LeftWheel.localPosition.y;
WheelY2 = RightWheel.localPosition.y;
Vector3 AxlePos = Axle.localPosition;
AxlePos.y = (WheelY1 + WheelY2);
Axle.localPosition = AxlePos;
Quaternion AxleAngle = Axle.localRotation;
AxleAngle.z = (Mathf.DeltaAngle (WheelY1 / WheelY2,5 / 355));
Axle.localRotation = AxleAngle;
}
}
Comment
Never $$anonymous$$ind I think I figured It out.
using UnityEngine;
using System.Collections;
public class TruckAxle : $$anonymous$$onoBehaviour {
public Transform LeftWheel;
public Transform RightWheel;
public Transform Axle;
public float WheelDistance;
public float WheelY1;
public float WheelY2;
public float BaseAdjust;
public float Adjust;
public Transform Xpoint;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
WheelY1 = LeftWheel.localPosition.y;
WheelY2 = RightWheel.localPosition.y;
Vector3 AxlePos = Axle.localPosition;
AxlePos.y = (BaseAdjust + WheelY1*Adjust + WheelY2*Adjust);
Axle.localPosition = AxlePos;
Quaternion AxleAngle = Axle.localRotation;
AxleAngle.x = ($$anonymous$$athf.DeltaAngle (WheelY1*-0.1f,WheelY2*-0.1f));
AxleAngle.z = ($$anonymous$$athf.DeltaAngle (0,Axle.localPosition.y*0.3f));
Axle.localRotation = AxleAngle;
}
}