- Home /
Make an object translate/raycast a certain way disregarding it's rotation
How can I make my object translate a certain way all the time, even when the rotation changes and how can I raycast always point down even when the object rotate?
So basically:
How can I make a raycast and translate ignore the rotation of the object?
Answer by sevensixtytwo · Sep 10, 2014 at 08:54 AM
Well you can use Vector3.down for the Ray's direction like so:
var direction : Vector3 = Vector3.down;
var ray : Ray = new Ray(origin, direction);
or
var direction : Vector3 = Vector3.down;
if (Physics.Raycast(origin,direction,distance)) {
//Do Something
}
This'll make the ray always shoot downwards regardless of an object's rotation.
For directions other than downwards, just get it like so:
var direction : Vector3 = (origin - target).normalized;
Assuming origin and target are Vector3.
UPDATE: For translating, it's a bit similar. Once you've got your direction, you use a line similar to this:
var speed : float = 10 * Time.deltaTime;
this.transform.Translate(direction * speed,Space.World);
The important part here is "Space.World". Essentially it tells the object to move along the global axis so regardless of its local rotation, it will move towards the indicated direction.
UPDATE 2: An attempt to translate these codes into C#:
Vector3 direction = Vector3.down;
Ray ray = new Ray(origin, direction);
//raycasts are called the same way in C# as in JS
if (Physics.Raycast(origin,direction,distance)) {
//Do Something
}
//same way with transform.Translate
float speed = 10 * Time.deltaTime;
this.transform.Translate(direction * speed,Space.World);
I'll see what I can do with this, do you have any idea how to do this for translating though?
Oops, much too focused on Raycasts. Updated the answer for translating.
I am still having some issues with the raycast, origin doesn't seem to exist for me?
I work in C# by the way.