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

Making an object casting a ray, rotate to face the direction of the hit

Discussion in 'Scripting' started by Bibi-Maghoo, Jul 7, 2014.

  1. Bibi-Maghoo

    Bibi-Maghoo

    Joined:
    Mar 18, 2013
    Posts:
    13
    Hi all, I have a bit of a tricky problem that I cannot seem to find an answer to in the documentation.

    I have an object that fires off physics.raycast in several directions. I want the object casting them to face the hit point once a hit is returned, but I have no idea how to register the object hit as a target for the casting object.

    So Enemy 1 raycasts forward, backwards, left and right, by 2000 float.
    Enemy 1 hits an object with the left raycast.

    I want enemy 1 to now turn and face that hit, so that it's left facing now becomes it's front facing. How can I achieve this?

    I know I can record the hit data with private var playerHit : RaycastHit, but have no idea now how to access that data, how to turn it into a direction and then act upon it.

    Any help you can provide would be great. I ask because I need a helping hand, not because I want a script written for me, so please be kind.
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Code (csharp):
    1.  
    2. var hit : RaycastHit;
    3.  
    4. if (Physics.Raycast (transform.position, Vector3.forward, hit, 2000))
    5. {
    6.      transform.LookAt(hit.transform.position);
    7. }
    8.  
    Is this what you mean?

    It might be easier and more practical if you use colliders instead of raycasts for this problem. You can use raycasts to check if there is anything in the viewfield though.
     
  3. Bibi-Maghoo

    Bibi-Maghoo

    Joined:
    Mar 18, 2013
    Posts:
    13
    Wait, so the variable that stores the hit info IS the target for rotation? I thought it would be much more complex, as the hit info variable is supposed to store a wide range of data from what I read. I thought I would need to somehow open up the info in that variable, rather than just declare the part I want?

    How then, for example, would I access where on a collider the hit occured? I don't need that, I'm just trying to understand what is stored in that variable, and how to access each part, but thank you, that is very helpful in my understanding. I will mark as correct, but if someone could explain my questions that would be fantastic.

    Edit: Lol, can't mark as correct, no in Q&A :D
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
  5. Tsilliev

    Tsilliev

    Joined:
    Jan 21, 2014
    Posts:
    34
    What if the raycast doesn't hit anything? Like you aim at the sky hoping to hit the flying mob?
    I am trying to align the fireball/bullet/rocket to the direction of being fired even while aiming at nothing .