- Home /
How to use two rays (ray casting)in one object?
Hi all I write this code for my game object but it doesn't work for that but when I delete the second part of that for example left hand side It work properly!! My main question is this: how can I use two rays in one object? And how can control my game object when it move between two another objects? When I use triggers because of the high speed of objects it don't work properly most of times. My Code:
var hSliderValue:int = 2;
var MovingSpeed : int = 2;
function Update ()
{
var hit : RaycastHit;
var right = -transform.TransformDirection(Vector3.forward);
var left = transform.TransformDirection(Vector3.forward);
transform.Rotate(0, 0, Input.GetAxis("Mouse X") * hSliderValue);
transform.Translate(0, 0, Input.GetAxis("Vertical") * MovingSpeed * Time.deltaTime);
//Compute the Right hand side
Debug.DrawRay(transform.position, right * 0.025, Color.green);
if(Physics.Raycast(transform.position, right, hit, 0.025))
{
if(hit.collider.gameObject.name == "right" && Input.GetAxis("Vertical") < 0 )
{
MovingSpeed = 0;
}
else
{
MovingSpeed = 2;
}
}
//Compute the left hand side
Debug.DrawRay(transform.position, left * 0.25, Color.red);
if(Physics.Raycast(transform.position, left, hit, 0.25))
{
if(hit.collider.gameObject.name == "left" && Input.GetAxis("Vertical") > 0 )
{
MovingSpeed = 0;
}
else
{
MovingSpeed = 2;
}
}
}
Excuse me dear friend. My game is 3d and in this scene we have 3 objects two of them in two sides like border and one of them in the middle. the middle object is moving some times fast and some times slow. At first time I use colliders but most of times in high speed they don't work and my middle object pass border objects. Because of that problem I change that to ray casting and now when I use it to just one side it work but when I use it for two sides it don't work for any side. you can imagine I have Foosball game and the middle object is player and two others is walls of table and player shouldn't pass the walls. I exactly don't know how to use multiple Raycasts at same time and think my code have problem.
Answer by alone1992 · Jan 30, 2013 at 11:29 AM
The problem is in the structure of my code I should put the left hand side code in the "else{}" of the right hand side or divide them to two part and put each one in separate scripts and next add them to my object
Answer by Bunny83 · Dec 08, 2012 at 12:24 PM
There's no problem using multiple Raycasts at the same time. However I'm not entirely sure what this code should do. I guess you want to stop the objects movement when it hits your "borders".
What do you mean with "don't work properly most of times". An explanation of what it should do would be great. Also we don't have your game scene. We don't know where the colliders named "left" and "right" are, what kind of game it is (2d / 3d).
The z-axis is usually forward in Unity but your raycasts are done to right and left (on the x-axis). Can you explain what's the purpose of the raycasts? Also a screenshot or scribble of your scene would be great. Feel free to edit your question
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Change skybox at runtime? 2 Answers
Getting audio.PlayOneShot to work 2 Answers
Safe area from enemies 1 Answer
Detect collision/trigger between two body without rigidbody? 3 Answers