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.