- Home /
Getting Vector3 in between two given Vector3 after given Distance.
I have a Camera Object and a Player Object separated; and given the circumstances I cannot include the Camera into the Player Object internal hierarchy.
Desired Scenario:
Retrieve a Vector3 position between the Camera and Player (each one with local rotations of their own, not in 0's) that has traveled 5 distance units starting from the Player Object.
float distance = 5.0F;
....
void Update() {
/* unknownVector3 should be in the same path as cam.transform
and player.transform but at distance of 5 starting from player and
towards camera */
cam.transform.position = Vector3.Lerp(cam.transform.position, unknownVector3, Time.deltaTime * 2.0f);
/* Below: Was using these lines but I'm hoping to achieve this in
one position adjustment per Update run */
cam.transform.position = player.transform.position;
cam.transform.Translate(new Vector3(0, 0, -distance ));
}
Thanks in advance people!
Answer by hexagonius · Oct 06, 2015 at 09:58 AM
Vector3 playerToCam = cam.transform.position - player.transform.position;
Vector3 fiveTowardsCam = player.transform.position + playerToCam.normalized * 5f;
Thanks @hexagonius. The code does exactly what I requested in my question.
Answer by Dave-Carlile · Oct 07, 2015 at 01:14 AM
Another option using Vector3.Lerp...
Vector3 fiveTowardsCam = Vector3.Lerp(player.transform.position, cam.transform.position, 5.0f / Vector3.Distance(player.transform.position, cam.transform.position));
Your answer
Follow this Question
Related Questions
How to teleport gameobject (instantly change transform.position) 3 Answers
Vector3.Lerp moves in the wrong direction 1 Answer
Child GameObject is aligned to world axis and not the parents axis (UPDATE) 1 Answer
Vector3.Distance is acting erratically 2 Answers
why one works, the other does not 2 Answers