Hello Unity Community and Moderators,
Im new, ive been messing with unity for a short time.
My situation: im writing some steering behaviours (in c#). Basically just creating forces and applying them to a moving object based on some rules (obstacle avoidance, etc.).
My question: im pretty new to 3d graphics programming and using physics so when i create a force and apply to an object does it apply to local or world space…
Here is a method for generating a random wander steer behaviour:
///////////////
// begin code
///////////////
public static Vector3 Wander(StearingObject currentObj)
{
//this behavior is dependent on the update rate, so this line must be included when using time independent framerate.
float JitterThisTimeSlice = currentObj.WanderJitter * Time.deltaTime;
//first, add a small random vector to the target’s position
//this is a random vector in 3 dimensions but the y is constrained to 0 so its actually in 2 dimensions.
currentObj.WanderTarget += new Vector3(Random.Range(-1, 1) * JitterThisTimeSlice, 0, Random.Range(-1, 1) * JitterThisTimeSlice);
//reproject this new vector back on to a unit sphere
currentObj.WanderTarget.Normalize();
//increase the length of the vector to the same as the radius of the wander circle
currentObj.WanderTarget *= currentObj.WanderRadius;
//move the target into a position WanderDist in front of the agent
Vector3 target = currentObj.WanderTarget + (currentObj.rigidbody.transform.forward * currentObj.WanderDistance);
// may have to project “target” to world space… dont know yet
Vector3 can be confusing for the reason that it can be multiple different things including actual Vectors (hence Vector3) and points in space.
So, if you are going to use Rigidbody.AddForce() then you will want to insert a Vector3 as a direction in worldspace. So if you want an object to move parallel to the x-axis, you would say AddForce(Vector3.right). If you want to move him forward in local space, then you would say AddForce(transform.forward)
In your case, you want a value in world space so that the object will be pushed in the direction of the Vector relative to the origin.
So if i understand you correctly i need to project the vector to world space before returning because the rigidbody member variables are all in local (object) space…?
I’m not sure which variables you are referring to, most are in world space, but for example. Let’s say you want to add force to the object so that it will move forward in local space.
(You can use AddRelativeForce() which does this under the hood) Unity has some built-in functions for turning local vectors into world vectors and vice-versa. So, first you take the local forward Vector and convert it into World Space. If your object is rotated, you might get something like Vector3( .3, .5, .1) which isn’t a real value, but you get the idea. Then you use AddForce with that Vector to propel the object in the world space equal of the local forward Vector.
function MoveObjForward () {
var forward : Vector3 = transform.TransformDirection(Vector3.forward); //Turn the local forward Vector into a World space Vector centered at the origin.
rigidbody.AddForce(forward * someNewtons); //This will push the object in the Direction of the world space vector forward. Forward is equal to the local forward vector.
}
Hope that makes more sense. It took me forever to conceptually understand when to use world vs. local space and when to convert them and etc.
I think the other thing you might be confusing is how Vectors are set up. A vector will not change if the object moves. So, the local right vector is still (1, 0, 0) no matter where the character is. The world space equal will still be something near the origin and have a magnitude the same as the local vector. The World space equal will also not change if the object moves. Vectors (in this case) are storing a direction not position so that does not affect the Vector.