Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question I need help about raycast.

Discussion in 'Scripting' started by MuhammetUgurBozdemir, Apr 17, 2021.

  1. MuhammetUgurBozdemir

    MuhammetUgurBozdemir

    Joined:
    Mar 6, 2021
    Posts:
    45
    I have a straight road and I instantiate a random cars on it.Every car has different speed, I attached a raycast front of them and I want if that ray touch any car in front car will brake(take speed of the car in front).But my code makes every car`s speed equal.How can I do this?

    Here is my code

    Code (CSharp):
    1.  
    2. public class EnemyCarMove : MonoBehaviour{  
    3.     public float speed;
    4.     public void Start()
    5.     {
    6.         speed = Random.Range(25, 45);
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     public void Update()
    11.     {
    12.         RaycastHit hit;
    13.         int layerMask = 1 << 8;
    14.  
    15.         if (Physics.Raycast(transform.position + new Vector3(0f, 0.5f, 0f), transform.forward, out hit, 5, layerMask))
    16.         {
    17.          
    18.             Debug.DrawRay(transform.position + new Vector3(0f, 0.5f, 0f), transform.TransformDirection(Vector3.forward) * hit.distance, Color.red);
    19.             Debug.Log("speed " + hit.transform.GetComponent<EnemyCarMove>().speed);
    20.             hit.transform.GetComponent<EnemyCarMove>().speed = speed;
    21.             Debug.Log("Target");        
    22.  
    23.         }
    24.         else
    25.         {
    26.             Debug.DrawRay(transform.position + new Vector3(0f, 0.5f, 0f), transform.TransformDirection(Vector3.forward) * 5f, Color.white);
    27.             Debug.Log("miss");
    28.         }      
    29.      
    30.         transform.Translate(0, 0, 1 * speed * Time.deltaTime, Space.World);
    31.     }  
    32.  
    33. }
    34.  
    35.  
     
    Last edited: Apr 17, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Look for inappropriate use of static values elsewhere that might control this.

    Also, strip your scene down the bare minimum number of cars to show off the problem, name every single object differently in scene, then instrument the code with copious amounts of Debug.Log() statements to find out what's happening.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you are running a mobile device you can also see the console output. Google for how.
     
  3. MuhammetUgurBozdemir

    MuhammetUgurBozdemir

    Joined:
    Mar 6, 2021
    Posts:
    45
    Thank you so much for advice.I tried but I can`t see the mistake because I`m a rookie yet. I add a video of the bug maybe this way will be more clear.
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    It doesn't look to me like your cars are slowing down when they're coming up on a car in front of them. It looks like they're just ramming into it at full speed and perhaps pushing that one along, which makes it seem like they're all moving at the same speed.

    Some of the best advice I can give you is to read and consider carefully every word of advice from @Kurt-Dekker. He's helped me with quite a few issues. In this case, you're trying to solve a problem that's difficult to understand when there's that much going on in your scene.

    I would literally reduce the number of cars in the scene to 2. Make sure they're in the same lane, and car up front is moving slower than the one behind it. Either add UI elements or Debug.Log statements to show each vehicle's speed, and another one that indicates when a car detects another vehicle in front of it. It'll be much easier to debug if you can watch what's happening and focus in on it.

    If that's too much for you to figure out, it's possible you're trying to do too much too quickly. You might need to back up a bit and go through some more tutorials or read the API documentation a bit more. I know you're excited to make your game idea, but you'll have a much better chance at succeeding with that if you're comfortable figuring out issues with the code yourself.

    Good luck!
     
    exiguous likes this.
  5. MuhammetUgurBozdemir

    MuhammetUgurBozdemir

    Joined:
    Mar 6, 2021
    Posts:
    45
    Thank you so much for all advices.I will try but I give up for now.Like you said , I need more tutorials and reading.Maybe If try another time I can do it better.
     
    Last edited: Apr 20, 2021
    Schneider21 likes this.
  6. MuhammetUgurBozdemir

    MuhammetUgurBozdemir

    Joined:
    Mar 6, 2021
    Posts:
    45
    I did it.If someone need samething with me this is the solution.

    Code (CSharp):
    1.     public void Update()
    2.     {
    3.        RaycastHit hit;
    4.        if(Physics.Raycast(transform.position,transform.forward,out hit, 20))
    5.         {
    6.             if (hit.collider.tag == "EnemyCar")
    7.             {
    8.                 Debug.DrawLine(transform.position, hit.point);
    9.                 hitDistance = hit.distance / 20;
    10.             }
    11.             else
    12.             {
    13.                 hitDistance = 1;
    14.             }
    15.         }
    16.         else
    17.         {
    18.             hitDistance = 1;
    19.         }
    20.  
    21.         transform.Translate(0, 0, 1 * speed * Time.deltaTime * hitDistance, Space.World);
    22.  
    23.     }