Hi i’m working on a car game with this tutorial
I’m on 3rd video and i’ve got this problem when i run the game
I tried with different cars but still the same
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Suspension : MonoBehaviour
{
public List<GameObject> springs;
public List<GameObject> wheels;
public Dictionary<GameObject, GameObject> points = new Dictionary<GameObject, GameObject>();
public Rigidbody rb;
public float maxForce;
public f loat maxDistance;
public float wheelRadius = 0.85f;
public float dampingFactor;
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < springs.Count; ++i)
{
points.Add(springs[i], wheels[i]);
}
}
void FixedUpdate()
{
foreach (GameObject spring in springs)
{
GameObject wheel;
bool found = points.TryGetValue(spring, out wheel);
RaycastHit hit;
if (Physics.Raycast(spring.transform.position, -transform.up, out hit, maxDistance))
{
float damping = dampingFactor * Vector3.Dot(rb.GetPointVelocity(spring.transform.position), spring.transform.up);
rb.AddForceAtPosition(maxForce * Time.fixedDeltaTime * transform.up * Mathf.Max(((maxDistance - hit.distance + wheelRadius) / maxDistance - damping), 0), spring.transform.position);
if (found)
wheel.transform.localPosition = new Vector3(wheel.transform.localPosition.x, (wheelRadius - hit.distance) / wheel.transform.lossyScale.y, wheel.transform.localPosition.z);
}
else if (found)
wheel.transform.localPosition = new Vector3(wheel.transform.localPosition.x, (wheelRadius - maxDistance) / wheel.transform.lossyScale.y, wheel.transform.localPosition.z);
}
}
}