How do I create a vector that points down from a cube that is rotating on all axis?

I have a cube that is rotating in local space on all axes and I would like to dray a ray cast from the cube straight down no matter it's rotation. I was trying:

var down = myTransform.TransformDirection(-Vector3.up);
Debug.DrawLine(myTransform.position, down * (20));

Any ideas? Thanks!

What's wrong with Herman Tulleken's answer?

5 Answers

5

Here you go:

Debug.DrawLine(myTransform.position, Vector3.down * (20));

You do not need any transformation. (Vector3 also has up, left, right, etc.)

[Edited Answer]

How about:

1) Creating a totally new gameObject, say DebugDrawLineObject.

2) Attach a script that has a function that draws the line directly downward in world space.

3) Make a reference to the rotating cube's position in said script.

This of course is a very crude and sort of inefficient way of 'simply drawing a debug line'.

[Old answer] How about:

1) creating an empty gameobject 'x' that would house that cube of yours.

2) Do the rotation on the cube (now parented with the object x), and

3) While the cube rotates in its own space, you are free to draw a line from object x downward. (since x itself shouldn't be rotating at all, downward would be straight down in world space)

Thanks for the reply! the cube is a rigidbody and its part of a ragdoll. Unfortunately the parent cube won't follow the rigidbody... Sorry I forgot to add this info :(

I see. Well.. what happens with the code you use, by the way? What direction does it draw the line?

It goes straight down from the cube's current rotation... :(

so the line itself is sort of "rotating" with the cube, yes?

edited my answer. good luck, I'm all out of ideas ;[

Debug.DrawLine(myTransform.position, transform.up*(-1*20));

Hmm this causes a line to go down from the cube's local space but not world space :(

Hey, I made an edit on my answer. =) this will work for you.

When you create the object, on start or awake , get the Value of line u want to make in the space , in this case vertically down. This vector should be stored as a const value, you could also consider making the Vector variable a static variable since it should not be inherited unless, having the demand the object needs to be leveled is hard to scan and should be implemented manually or derived as whole functioning system. This line will now never change in the code , if you do you will get an error report.

private const Vector3 myVector = //Your wanted Vector;

Now that you have the Vector information you can continue re-using that as your origin. It does not make sense to try to use the rotating object itself as a reference point, you need to store the true value or use an orientation point but never a source that is rotating it's own orientation since it can never get that information.

Don't forget you can not ignore initializing const values or they will become pointless since the empty container can no longer be changed.

If you are going to use something like Vector3.up or Vector3.down , you are required to set the object's original axis value to have no rotation over x or z axes or you will not set a leveled value in the object to shoot straight down. Take into consideration if this is something you want to do with your object, it can easily be overlooked, or simply unknown to other users of your code

You can't make classes or structs const. You could use static readonly, but honestly Vector3.up and transform.up are pretty self documenting

The problem is they rely on the orientation on the object, and my answer may not have been clear enough. I will edit the static

sorry for bumping this old post but it took me hours to figure it out:

Debug.DrawRay (transform.position, Vector3.down * 20, Color.green);