Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Help

Discussion in 'Scripting' started by tsapapolis, Jan 18, 2021.

  1. tsapapolis

    tsapapolis

    Joined:
    Nov 27, 2020
    Posts:
    3
    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
    View attachment 776188
    I tried with different cars but still the same


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class Suspension : MonoBehaviour
    6. {
    7.     public List<GameObject> springs;
    8.     public List<GameObject> wheels;
    9.     public Dictionary<GameObject, GameObject> points = new Dictionary<GameObject, GameObject>();
    10.     public Rigidbody rb;
    11.     public float maxForce;
    12.     public f loat maxDistance;
    13.     public float wheelRadius = 0.85f;
    14.     public float dampingFactor;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         for (int i = 0; i < springs.Count; ++i)
    20.         {
    21.             points.Add(springs[i], wheels[i]);
    22.         }
    23.     }
    24.  
    25.     void FixedUpdate()
    26.     {
    27.         foreach (GameObject spring in springs)
    28.         {
    29.             GameObject wheel;
    30.             bool found = points.TryGetValue(spring, out wheel);
    31.  
    32.             RaycastHit hit;
    33.             if (Physics.Raycast(spring.transform.position, -transform.up, out hit, maxDistance))
    34.             {
    35.                 float damping = dampingFactor * Vector3.Dot(rb.GetPointVelocity(spring.transform.position), spring.transform.up);
    36.                 rb.AddForceAtPosition(maxForce * Time.fixedDeltaTime * transform.up * Mathf.Max(((maxDistance - hit.distance + wheelRadius) / maxDistance - damping), 0), spring.transform.position);
    37.  
    38.                 if (found)
    39.                     wheel.transform.localPosition = new Vector3(wheel.transform.localPosition.x, (wheelRadius - hit.distance) / wheel.transform.lossyScale.y, wheel.transform.localPosition.z);
    40.             }
    41.             else if (found)
    42.                 wheel.transform.localPosition = new Vector3(wheel.transform.localPosition.x, (wheelRadius - maxDistance) / wheel.transform.lossyScale.y, wheel.transform.localPosition.z);
    43.         }
    44.     }
    45. }
    46.  
     
  2. GGfazo

    GGfazo

    Joined:
    Sep 18, 2020
    Posts:
    3
    I'm not sure but I think it could be detecting the car with the raycast
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Easy to check. Add some Debug.Log statements after the raycast and print out the name of the object you're hitting.
     
    Kurt-Dekker likes this.