0
\$\begingroup\$

How to make an object stay at end of the raycast if the ray hit nothing ?! Here is my script

using UnityEngine;
using System.Collections;

public class ray : MonoBehaviour {

public float howFar;
public Vector3 loaction;
public Transform spot_position;
public Vector3 loaction_air;

void Start() {

}

void FixedUpdate()  {
    Vector3 fwd = transform.TransformDirection(Vector3.forward);
    RaycastHit hit;

    if (Physics.Raycast(transform.position, fwd, out hit, 100)) {
        print("Ray hit something !");
        loaction = hit.point;
        howFar = hit.distance;
        spot_position.transform.position = hit.point;
    }
    else {
  //            howFar = ....
 //         spot_position.transform.position = ...
        print (" Nothing ");
    }

    loaction_air = new Vector3(spot_position.transform.position.x,spot_position.transform.position.y,spot_position.transform.position.z);
}

}
\$\endgroup\$
1
\$\begingroup\$

I think this should give you the desired effect:

if (Physics.Raycast(transform.position, fwd, out hit, 100)) {
    spot_position.transform.position = hit.point;
}
else {
    spot_position.transform.position = transform.position + fwd* 100; 
}

Also, if you are new to Unity I think there are some helpful tips that you may not know yet.

1/ You can easily get the forward vector of the object with transform.forward.

2/ transform.position returns a new copy of object's position so just get position of object with transform.position and do what you want with that copy, the object's position remains unaffected.

3/ If you have the reference to the Transform component, then there is no need to refer to transform again, just use that reference as a Transform normally.

Lets simplify your code using those tips above:

using UnityEngine;
using System.Collections;

public class Ray : MonoBehaviour 
{ 

    public float howFar;
    public Vector3 location;
    public Transform spotPosition;
    public Vector3 locationAir;

    void Start() 
    {

    }

    void FixedUpdate() 
    {
        var fwd = transform.forward;
        var distance = 100f;
        RaycastHit hit;
        if (Physics.Raycast(transform.position, fwd, out hit, distance )) {
            Debug.Log("Ray hit something !");
            location= hit.point;
            howFar = hit.distance;
            spotPosition.position = hit.point;
        } else 
        {
            Debug.Log(" Nothing ");
            howFar = distance;
            spotPosition.position = transform.position + fwd* distance; 
        }

        locationAir = spotPosition.position;
    }
}
\$\endgroup\$
1
\$\begingroup\$

To call the raycast you have to give the fuction the direction and distance parameters.

Just use this information, and add the normalized direction (will be already normalized if you are using forward) multiplyed by the distance to the object position:

itemToMove.transform.position += transform.Forward.normalized * distance

\$\endgroup\$
  • \$\begingroup\$ your code is work but its not what i want. I need the object to stay at end of the ray. this ray start out from my camera (Stay at middle of my camera). So, it move with it. \$\endgroup\$ – user43474 Sep 20 '16 at 19:17
  • \$\begingroup\$ Could you provide screenshots about what is happening, and what you are trying to achieve? \$\endgroup\$ – Leo Sep 20 '16 at 20:13
  • \$\begingroup\$ s16.postimg.org/pi47s83tx/image.png else { spot_position.transform.position = spot_position.position; print (" Nothing "); } \$\endgroup\$ – user43474 Sep 20 '16 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.