# can we use multiple raycast function in one object?

Hi,

i have used four raycast function in one object but only one function only work correctly i dont how multiple raycast work. Help me… here is my code…

``````using UnityEngine;
using System.Collections;

public class RayCaast_Test : MonoBehaviour {

public bool repul;
public Vector3 RepulForce;
private Vector3  lastpos;

// Use this for initialization
void Start () {

repul = false;

}

// Update is called once per frame
void Update () {

transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * 5, 0.0f, Input.GetAxis("Vertical") * Time.deltaTime * 5, Space.World);

//Ray ray = Camera.main.ScreenPointToRay(transform.position);

RaycastHit hit;

if(Physics.Raycast(transform.position, Vector3.left, out hit, 0.5f)){

if(hit.collider.gameObject.name == "Sphere"){

repul = true;

print("This is the left  raycst function"+hit.distance);

}
}

else if(Physics.Raycast(transform.position, Vector3.right, out hit, 0.5f)){

if(hit.collider.gameObject.name == "Sphere"){

repul = true;

print("This is the right raycst function");

}
}

else if(Physics.Raycast(transform.position, Vector3.up, out hit, 0.5f)){

if(hit.collider.gameObject.name == "Sphere"){

repul = true;

print("This is the up raycst function");

}
}

else if(Physics.Raycast(transform.position, Vector3.forward, out hit, 0.5f)){

if(hit.collider.gameObject.name == "Sphere"){

repul = true;

print("This is the foward raycst function");

}
}

else if(Physics.Raycast(transform.position, Vector3.back, out hit, 0.5f)){

if(hit.collider.gameObject.name == "Sphere"){

repul = true;

print("This is the back raycst function");

}
}

else if(Physics.Raycast(transform.position, Vector3.down, out hit, 0.5f)){

if(hit.collider.gameObject.name == "Sphere"  hit.collider.gameObject.name != "Plane"){

repul = true;

print("This is the down raycst function");

}
}

if(repul){

print("Tis is the repul force true function");
}

else{

print("This is the repul force false function");
}

}
}
``````

is this possible to do?

You’re using if…else statements. They’re evaluated in the order they’re written and the lower ones are only evaluated if the above one is false. So i suspect what’s happening is your first raycast is hitting the sphere, returning true and its skipping the rest. Take out the else from in front of each if and it should work as you want it to.

ok i will check this out as you said. let me tell you after that.

@Swiftpaw no your logic does not work… any other ideas?

It does work, you must have an error somewhere in your code. Here’s a quick example that does what you’ve described:

``````    using UnityEngine;

using System.Collections;

public class RayCastExample : MonoBehaviour {

public bool hitSomething;

public float traceDistance; // distance to trace, change as needed in inspector

// Use this for initialization
void Start () {
hitSomething = false;
}

// Update is called once per frame
void Update () {

transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * 5, 0.0f, Input.GetAxis("Vertical") * Time.deltaTime * 5, Space.World);

RaycastHit hit;

if(Physics.Raycast(transform.position, Vector3.left, out hit,traceDistance)){
if(hit.collider.gameObject.name == "Sphere"){
hitSomething = true;
print("Collided Left");
}
}
if(Physics.Raycast(transform.position, Vector3.right, out hit, traceDistance)){
if(hit.collider.gameObject.name == "Sphere"){
hitSomething = true;
print("Collided Right");
}
}

if(Physics.Raycast(transform.position, Vector3.up, out hit, traceDistance)){
if(hit.collider.gameObject.name == "Sphere"){
hitSomething = true;
print("Collided Above");
}
}

if(Physics.Raycast(transform.position, Vector3.forward, out hit, traceDistance)){
if(hit.collider.gameObject.name == "Sphere"){
hitSomething = true;
print("Collided Forward");
}
}

if(Physics.Raycast(transform.position, Vector3.back, out hit, traceDistance)){
if(hit.collider.gameObject.name == "Sphere"){
hitSomething = true;
print("Collided Behind");
}
}

if(Physics.Raycast(transform.position, Vector3.down, out hit, traceDistance)){
if(hit.collider.gameObject.name == "Sphere"  hit.collider.gameObject.name != "Plane"){
hitSomething = true;
print("Collided Below");
}
}
if(hitSomething){
// Do something
}
else{
// Do something else
}
}
}
``````

The above code is tested and works, so compare it to what you have and see if there’s an error somewhere, you should be able to get it with a bit of fiddling around, have fun!

ya its working…

So why didn’t you thank Swiftpaw?

2 Likes