LookAt only rotates on one axis inside particular block of code
Hi! This is probably super simple but it's 2.30am and I'm shot so hopefully you can put me out of my misery.
I have a teleport system set up. When a player collides with the teleport, they are placed at an "Out Point" in front of the exit. I'd like them to also be facing the correct direction. The way I've been attempting this is to have an empty child object of the exit teleporter, in the direction I'd like the player to face, and using LookAt to make the player face that object.
So I have
public Transform gameCamera;
private Transform lookTarget;
[...]
if(collision.collider.tag == "Teleport")
{
Transform outPoint = collision.collider.transform.Find("OutPoint").transform;
Transform oppositePort = collision.collider.GetComponent<Teleport>().oppositePort;
this.transform.position = outPoint.position;
lookTarget = oppositePort.Find("Front").transform;
gameCamera.transform.LookAt(lookTarget);
}
Problem: this only rotates the camera around the Y-axis, so it will face the direction of the child object but won't look up or down to find it.
However, when I do this:
public Transform gameCamera;
private Transform lookTarget;
void update()
{
gameCamera.transform.LookAt(lookTarget);
[...]
if(collision.collider.tag == "Teleport")
{
Transform outPoint = collision.collider.transform.Find("OutPoint").transform;
Transform oppositePort = collision.collider.GetComponent<Teleport>().oppositePort;
this.transform.position = outPoint.position;
lookTarget = oppositePort.Find("Front").transform;
}
}
the camera will happily rotate up and down to face the object as soon as it is set by the player walking through the teleporter.
In other words, LookAt is only changing the Y-axis when I issue the command once and is changing all relevant axes when I issue the command every frame. I have no clue why this is. Thanks for any help you can offer. :)
Your answer
Follow this Question
Related Questions
UNET Clients Laggy 0 Answers
Loot table problems 1 Answer
Rotation script issue? 2 Answers
Game crashes on iPhone 11 0 Answers
Play animation 10 seconds, after play other animation 1 Answer