- Home /
CS1061 Racing Game Tutorial
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WheelController : MonoBehaviour
{
public bool steer;
public bool invertsteer;
public bool power;
public float SteerAngle { get; set; }
public float Torque { get; set; }
private WheelCollider wheelCollider;
private Transform wheelTransform;
void Start()
{
wheelCollider = GetComponentInChildren<WheelCollider>();
wheelTransform = GetComponentInChildren<MeshRenderer>().GetComponent<Transform>();
}
void Update()
{
wheelCollider.GetWorldPose(out Vector3 pos, out Quaternion rot);
wheelTransform.position = pos;
wheelTransform.rotation = rot;
}
public void FixedUpdate()
{
if (steer)
{
wheelCollider.steerAngle = SteerAngle * (invertsteer ? -1 : 1);
}
if(power)
{
wheelCollider.motorTorque = Torque;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class car : MonoBehaviour
{
public Transform centerOfMass;
public float motorTorque = 1500f;
public float maxSteer = 20f;
public float Steer { get; set; }
public float Throttle { get; set; }
private Rigidbody _rigidbody;
private Wheel[] wheels;
void Start()
{
wheels = GetComponentsInChildren<Wheel>();
_rigidbody = GetComponent<Rigidbody>();
_rigidbody.centerOfMass = centerOfMass.localPosition;
}
void Update()
{
foreach (var wheel in wheels)
{
wheel.SteerAngle = Steer * maxSteer;
wheel.Torque = Throttle * motorTorque;
}
}
}
Following this right now - while it seems to work for him, i get two Errors when writing "wheel.SteerAngle = Steer * maxSteer;" What am I doing wrong? (line 74)
Are those classes in different files? if not -> monobehaviours need their own files.
Why did you add the "WheelController" Class? Post the Wheel
class since that one seems to cause the issue.
Also in general please do not write down Error codes except if they are really important. The actual error message is better since pretty much noone knows the error codes and every needs to look up what the actual error message would be.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
I want to learn more 1 Answer
SmartFoxServer or any multiplayer system installation 0 Answers
How to have realistic collisions with AI in a racing game? 2 Answers