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

Offset raycast origin!

Discussion in 'Scripting' started by ThisIsSparta, Aug 18, 2016.

  1. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    Hi guys! I hope someone can help me because I'm from a while stuck on this.

    I need to change the raycast origin of the ray casting from the character. Here is the code

    Code (CSharp):
    1.     wallRay = new Ray (transform.position, transform.forward);
    2.             RaycastHit hitWall;
    3.             Debug.DrawRay(transform.position,transform.forward, Color.red, 2);
    4.             if(Physics.Raycast(wallRay, out hitWall))
    5.             {
    6.                 if (hitWall.collider.tag == "Walkable") {
    7.                     positionToMoveTo = hitWall.collider.transform.position;
    8.                     StartCoroutine("PositionChanging");
    9.                     Debug.Log ("Muro!");
    10.                 }
    I tried everything I found on the forums but can't find nothing that I can make it work -.-

    any help will be appreciated! thx!
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    Which direction are you trying to offset it? If you replace the wallRay line with...

    Code (csharp):
    1. wallRay = new Ray(transform.position + transform.forward, transform.forward);
    this will move your origin in the character's forward direction by 1 unit.
     
    ackast3 likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    I'm trying to move it upward on the y axis because at the moment is casting from the character's feet
     
  5. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    well thank you very much! I did it using transform.up instead of transform.forward. Everything working now! But I'm curious about...what if I want to control the units? I mean like...at the moment is up by one unit; what if I want like 0,5? creating a new vector 3?
     
  6. Abdullah130

    Abdullah130

    Joined:
    Sep 8, 2020
    Posts:
    3
    Thank you guys.