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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Raycasting specifics

Discussion in 'Scripting' started by GFFG, Aug 7, 2015.

  1. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    I'm trying to ray cast from the centre of an enemy to my player and I've been following a tutorial that explains that ray casting happens from the pivot point of the object with the script on it.

    Since the pivot point of my enemy character is at its feet, the tutorial says the following code moves the ray cast up 1 "unit" which would be approximately the midpoint of the character in the tutorial because that character is 2 units. Mine is a lot smaller and thus the whole easy peasy unit thing doesn't work. this is the line of code to move the ray cast up one unit:
    Code (CSharp):
    1.                     if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius))
    2.  
    I either need to move my pivot point of the enemy character or i need to know how to write the code pasted above in such a way that i can control how high up the ray cast is transformed. help would be very welcome :)
     
  2. Lorinthio

    Lorinthio

    Joined:
    Aug 7, 2015
    Posts:
    39
    the transform.up, I believe is simply a Vector3(0, 1, 0);

    So change transform.position + transform.up, to...

    transform.position + new Vector3(0, height, 0);

    where height, is the Y offset you need!
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Transform.up and Vector3.up are not the same thing, and if you're going to add to the current position you need to use Vector3.up. You can take Lorinthio's approach and make a new Vector3 with the "y" being the height value you wish, or you just multiply the Vector3.up by the percentage height difference, like:
    Code (csharp):
    1. if(Physics.Raycast(transform.position + (Vector3.up * height), transform.forward, out hit, col.radius))
    or something. If you want to know the difference between Vector3.direction and Transform.direction, first check the difference between local and world space coordinates, and maybe look at the manual page for Transform.TransformDirection too. Transform.up, for instance, is equivalent to Transform.TransformDirection(Vector3.up), but shorthand.
     
    Last edited: Aug 7, 2015
  4. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Thank you very much, very informative. I'll look into it ;)
     
  5. Lorinthio

    Lorinthio

    Joined:
    Aug 7, 2015
    Posts:
    39
    Thanks for the clarification!
     
    Last edited: Aug 7, 2015