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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[SOLVED] Unity get distance between wall and car from 5 directions

Discussion in 'Scripting' started by Raiyanetohanoku, Dec 28, 2019.

  1. Raiyanetohanoku

    Raiyanetohanoku

    Joined:
    Nov 13, 2018
    Posts:
    2
    I want to get the distance between the wall and a car from 5 directions : front, left, right, forwardleft and forwardright. Can anyone please help me how to script that in unity?
     
  2. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    56
    I'm pretty new at unity too. But I'd like to help.

    You want to look into Physics.Raycast. It's a way to shoot a ray from an origin point into a certain direction. It can return a point where the ray hit.

    So you have an origin (your car) and a hit point (where the ray hit a wall or something).

    Subtract these two Vector3's. Now you get the difference between the two V3's.

    Look into what .magnitude does with a V3. It gives the length of the vector (the distance).

    Then use the given distance for what you intended to do with it.

    Do this for all 5 spots surrounding your car.
     
    Raiyanetohanoku likes this.
  3. Raiyanetohanoku

    Raiyanetohanoku

    Joined:
    Nov 13, 2018
    Posts:
    2
    Yes I managed to do it.. Thanks a lot :D