- Home /
Get the position behind an object (locally) ?
Hey guys, I ran into a problem while making my complex(ish) camera behaviour. The problem that i have run to is that i am not able to get the position behind my player. Please don't tell me to use Vector3.back etc.. , because i want the position locally, to support turning. It won't work if the camera is always set to that position, cos i have a cool movement system in place.
I tried a number of approaches, and confused myself with the math. I also tried complex addition and subtraction. None of them worked at all.
I guess i am probably missing something quite simple, like a way to get into local coordinates, or etc..
Please be more specific. Is your game 2D or 3D? top-down or third person?
Answer by rutter · Jun 05, 2014 at 05:48 PM
Every GameObject has a Transform component, which can give you its local right, up, and forward vectors. In particular, multiplying local forward by -1
will give you a "backwards" direction.
The following would give you a point one unit behind your target:
function GetBehindPosition(target : Transform) {
return target.position - target.forward;
}
Because the forward/right/up vectors are normalized (have a length equal to one), you can multiply them by any number X
to get a point X
units in that direction:
function GetBehindPosition(target : Transform, distanceBehind : float) {
return target.position - (target.forward * distanceBehind);
}
You can even mix and match your offsets:
function GetBehindPosition(target : Transform, distanceBehind : float, distanceAbove : float) {
return target.position - (target.forward * distanceBehind) + (target.up * distanceAbove);
}
$$anonymous$$ight I ask what would be the same solution in 2D space of your second piece of code (x units of distance behind given object)? Relative to the targeting object's position (the 'behind' should change, depending on the position of the targeting object, regardless of where the target is facing).
Your answer
![](https://koobas.hobune.stream/wayback/20220613150851im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using custom Transform/GameObject classes? 0 Answers
Calculate a new position instead of using Vector3.back 1 Answer
Creating a bouncing game without using physics - Vector3 math problem, 2 Answers
mousePosition.x value type. (Equations involving coordinates) 1 Answer
Transform vector to other coordinate frame using quaternions 1 Answer