Search Unity

C#, how can I Physics.Raycast from a position other than the object origin?

Discussion in 'Scripting' started by From-Soy-Sauce, Jul 23, 2014.

  1. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162

    Hello, I wish to be able to use Physics.Raycast to shoot 2 parallel rays at positions x+-.5 relative to the capsule like shown in figure b.

    Currently I only know how to make it like in figure A though. This is the code I use:

    Code (CSharp):
    1. float dist1 = 0;
    2. RaycastHit hit;
    3. if (Physics.Raycast(transform.position, -Vector3.up, out hit))
    4. dist1 = hit.distance;
    5. print(dist1);
     
  2. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    What you could do would be to have a few Vector3 variables defined at the top of the code, and then update them every frame. See below:

    Code (csharp):
    1.  
    2. Vector rayStart1, rayStart2, myPos;     // rayStart1 and rayStart2 are where the rays will start
    3.  
    4. void Start()
    5. {
    6.    myPos = transform.position;    // caching transform.position
    7.    rayStart1 = myPos;
    8.    rayStart2 = myPos;
    9.    rayStart1.x -= 0.5f;                   // adjusts the x-coordinate
    10.    rayStart2.x += 0.5f
    11. }
    12.  
    13. // called once a frame
    14. void Update()
    15. {
    16.    myPos = transform.position;
    17.    rayStart1 = myPos;                // resets rayStart1 to the object's currentposition
    18.    rayStart1.x -= 0.5f;                 // decreases the x-coordinate by 0.5 during the same frame
    19.    rayStart2 = myPos;               // repeat for rayStart2
    20.    rayStart2.x += 0.5f
    21.  
    22.   // call your raycasting code here, once for each ray
    23. }
    24.  
     
  3. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Instead of hardcoding a position offset, create 2 empty gameobjects and attach them to either side of your player, whereever you want the casts to start. Then pass those into your script.

    It's a lot more scalable and a lot easier to change.
     
    Philip-Rowlands and joni-giuro like this.
  4. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Those are both interesting answers, I like the first one better (because I love being able to do things using JUST code). I do want to ask a bit more of a detailed question for some clarity though.

    in:
    Physics.Raycast(transform.position, -Vector3.up, out hit)

    The first argument is for the position right? Is there really no way to have the differences of position stated somewhere in there? This is just pseudo code to illustrate it, but something that would work kinda like this? :
    Physics.Raycast(transform.position(x-.5f, y, z), -Vector3.up, out hit)
     
  5. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Seeing as the major advantage of using an engine like unity is the opposite of this, it doesn't make any sense.


    Code (CSharp):
    1. Physics.Raycast(new Vector3(transform.position.x - 5.0f,transform.position.y,transform.posiiton.z), -Vector3.up, out hit);
     
  6. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    Yeah, that's definitely a better solution.
     
  7. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    What if you had 10 different AI types that all used the same script but prefabs were all different? But you wanted to change one position. You'd have to manually reset all 10 of those game objects. It also makes the object more messy. As projects get larger and larger dealing with Objects should be minimized.

    I think the code approach is the better solution for modifying the Raycast position. Make it a Vector3 you can add to your current position.. Then you can handle it in the inspector.