- Home /
Using TransformDirection incorrectly?
I'm trying to set up a bool identifying which direction on the X-axis a vehicle is moving (right or left). I figured TransformDirection is how I would go about doing this. Yet I get this error: "An object reference is required to access non-static member 'UnityEngine.Transform.TransformDirection(UnityEngine.Vector3)' ". Here is my script:
private bool goingLeft;
private bool goingRight;
void Update();
goingRight = Transform.TransformDirection(Vector3.right);
goingLeft = Transform.TransformDirection(Vector3.left);
if(goingRight){
Debug.Log("Going right!");
}
if(goingLeft){
Debug.Log("Going left!");
}
First off, don't use capital T on transform when you are referring to the component. Second, transform.TransformDirection is a Vector3, not a bool. Bool is a true / false. Before I can actually answer this, I need to know, how are you moving your object??? Is it a rigidbody? Or are you moving it with Translate or Position+?
The vehicle is a rigidbody that only moves left or right on the X-axis (it's a 2D perspective) and it's moved with motorTorque and Wheel Colliders
Answer by clunk47 · Sep 24, 2013 at 02:28 AM
Since it's a rigidbody, check out Rigidbody.velocity.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class example : MonoBehaviour
{
bool goingLeft;
bool goingRight;
void Update()
{
if(rigidbody.velocity.x > 0)
goingRight = true;
else
goingRight = false;
if(rigidbody.velocity.x < 0)
goingLeft = true;
else
goingLeft = false;
if(goingRight)
Debug.Log("Going right!");
if(goingLeft)
Debug.Log("Going left!");
}
}
I've tried Rigidbody.velocity, it works accordingly but when the vehicle brakes the log will show GoingLeft/Right alternating continuously.
The vehicle's chassis does bounce and wobble due to the spring settings of wheel colliders (which is intended), could this be the cause? And will it affect the use of this script?
Try adjusting the velocity statement. Ins$$anonymous$$d of > or < than Zero, try 1.0f, 0.5f, 5.0f, depending on the max velocity, just experiment around with the velocity statement to find what speed you need to check to deter$$anonymous$$e left or right, starting with smaller numbers and moving up accordingly.
Alright, thanks I'll mess around some more with it. You pretty much answered my question, if I have further difficulties with the vehicle I'll just ask a new question. Thank you to everyone else, as well
Just as an update, tinkering the value of the velocity statement results in the desired affect. Thanks again!
Answer by robertbu · Sep 24, 2013 at 02:07 AM
'Transform' is the class. You want the 'transform' which is the transform of the specific game object. Note there are some shortcuts. 'transform.right' is the same as transform.TransformDirection(Vector3.right). And for left, you can use '-transform.right'.
Your next problem is that Transform.TransformDirection() returns a Vector3, not a bool, and it does not return the direction you are heading. How will solve this problem will depend on how you are moving the object. If you object is facing in the direction of movement, you can use either Vector3.Dot() or Vector3.Angle() and compare Vector3.left and Vector3.right to figure out which direction it is going. If the object is not rotated, then you will have to calculate the velocity by subtracting the position of the previous frame. Checking the sign of the 'x' axis of the velocity will give you the direction the object is moving.