Which component of a velocity Vector3 controls the direction. And how would I change it without effecting the magnitude? Thanks.
The whole vector indicates the direction and the speed.
to get the direction as a unit vector do this:
Vector3 direction = velocity.normalized;
To get the speed do this:
float speed = velocity.magnitude;
To change a direction without effecting the magnitude you could do something like this:
Vector3 desiredDirection = new Vector3(0,0,1); // set this to the direction you want.
Vector3 newVelocity = desiredDirection.normalized * currentVelocity.magnitude;
If you want to change the direction over time then you could try this method in an Update function:
float rotationSpeedDegrees = 180; // 180 degrees per second.
Vector3 newVelocity = Vector3.RotateTowards( currentVelocity, desiredDirection, rotationSpeedDegrees * Time.deltaTime * Mathf.Deg2Rad, 0 );
And for physics applying a force perpendicular to the current velocity will cause the direction of the velocity to change without affecting the magnitude.
The thing I like to do with physics is use:
Vector3 newVelocity = new Vector3(0,0,1); // calculate some new velocity you want your object to go at.
Vector3 currentVelocity = rigidbody.velocity; // get the current velocity
rigidbody.AddForce( newVelocity - currentVelocity, ForceMode.VelocityChange ); // add the difference.
@Antony-Blackett
Thanks, I’ve got it working, mostly. My problem is defining the new vector. Using your example of (0,0,1) it more or less goes straight. And (0,1,0) straight up in the air, as expected. I’ve been experimenting with values without much success. I looked for a vector calculator online, but nothing was an obviously a solution. Can you point me in the right direction?
Sounds like you might need to read up on Vector maths, unfortunately I don’t know of any very simple and easy to follow Vector math tutorials that’ll help you get started. There’s really not a lot to it and it is very worthwhile to learn as I use it almost every hour of every day. I can help you out a bit but I currently don’t know what you want to achieve, can you explain a little more about what it is you’re trying to accomplish.
I have a vehicle traveling across the scene from point 1 to point 2. When it gets to point 2 I’m ‘teleporting’ it back to point 1 with a trigger and a transform location. But it maintains it’s same trajectory and shoots of the road. I need to modify the direction to follow the blue line. I actually need to set up this system at the ends of each road. So what would be ideal would be for me to set it up so the vector followed the +X direction of the destination transform. That way I could just rotate the destination in each prefab to align with the road. I will learn vector math, but any direction would be greatly appreciated. Thank you for your time.
public Transform Destination;
private VehicleController vehicleControler = null;
private Rigidbody vehicle;
private Vector3 desiredDirection = new Vector3(0, 0, 1);
private float currentVelocity;
void OnTriggerEnter(Collider other)
{
vehicleControler = other.GetComponentInParent<VehicleController>();
vehicle = vehicleControler.GetComponent<Rigidbody>();
currentVelocity = vehicle.velocity.magnitude;
vehicle.transform.position = Destination.transform.position;
vehicle.velocity = desiredDirection.normalized * currentVelocity;
}
Well that was WAY easier than my math teacher made it out to be 20 years ago. The code below works, but it’s a little messy. Is there a library that I can use to simplify the angle calculations? Is there a cleaner way to do this? Thanks again.
public Transform Destination;
private VehicleController vehicleControler = null;
private Rigidbody vehicle;
private Vector3 desiredDirection;
private Quaternion zeroRotation = Quaternion.Euler(0, 0, 0);
private float currentVelocity;
private float angle;
void OnTriggerEnter(Collider other)
{
vehicleControler = other.GetComponentInParent<VehicleController>();
vehicle = vehicleControler.GetComponent<Rigidbody>();
currentVelocity = vehicle.velocity.magnitude;
angle = Quaternion.Angle(Destination.rotation, zeroRotation);
desiredDirection = new Vector3(Mathf.Cos(Mathf.Deg2Rad * angle), 0, Mathf.Sin(Mathf.Deg2Rad * angle));
vehicle.transform.position = Destination.transform.position;
vehicle.velocity = desiredDirection.normalized * currentVelocity;
}
There is a much easier way.
Transforms have some handy vector properties you can use.
try something like this:
vehicle.velocity = Destination.forward * currentVelocity;
you could also do it another way using Vector maths if you had the next destination transform available to you:
Transform NextDestination = someTransform; // set this up yourself.
Vector3 toDestination = NextDestination.position - vehicle.position; // get the vector from the current position to the next destination.
Vector3 velocityDirection = toDestination.normalized; // normalize the toDirection vector so we can scale it easily to our current velocity magnitude.
float velocityMagnitude = currentVelocity; // what you should probably call this variable (currentVelocity) as the word velocity implies a direction where as this is just a magnitude (e.g. a speed or length, etc).
vehicle.velocity = velocityDirection * velocityMagnitude;
and this can all be simplified into a single line of code, but note that single lines of code are not really any more efficient and are also harder to debug, but they can make code easier to read sometimes.
vehicle.velocity = (someTransform.position - vehicle.position).normalized * currentVelocity;
Hope this helps.
Sorry, I didn’t see your road image before and I didn’t realise your roads are curved. To get an object to follow a curve you’ll probably want to look into what’s called a Spline.
There are a number of spline tools in the asset store that could make this easier for you.
Another thing you could try is to animate the car along the road using an animator component and the animation window.
This was a great solution! The curved road was only an issue until I figured out the velocity thing. The rigidbody i’m working with is part of a car controller, so I can drive it where I want. I’m using splines to move other cars around simulating traffic. Thanks again.