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!
Here you go:
Debug.DrawLine(myTransform.position, Vector3.down * (20));
You do not need any transformation. (Vector3 also has up, left, right, etc.)
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'.
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)
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
sorry for bumping this old post but it took me hours to figure it out:
Debug.DrawRay (transform.position, Vector3.down * 20, Color.green);