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");
}
}
}