- Home /
Strange thing happening
Hello. I am trying to make the main camera to follow the player. Offset is currently set to 0, 0, 0. So it should place camera right into the centre of my player (cube). But instead, this happens: player position is 0, 1, 0. Camera position sets to 0, 1.48, -6.
I tried
public Transform player;
public Transform Camera;
public Vector3 offset;
void Update()
{
Camera.position = player.position + offset;
}
and
public Transform player;
public Vector3 offset;
void Update()
{
transform.position = player.position + offset;
}
both have the same result
Well, if someone of you could help me out, I would be happy. But could someone try to teach me a bit? Maybe teaching is not the right word. Just a help here and there (maybe more in the beginning). I watched and followed some YT videos but I just feel like I did not learn anything and I am just copying... but when I try to create something on my own it never works. I would be happy if someone helped me out.
Thank you for your time.
Answer by spidermancy612 · Jan 19, 2018 at 05:18 PM
I jumped into Unity and tried out your code, and it worked for me. So we know your code works, which means it something else.
~
The first thing I would say is that in the first chunk of code you have, the word "Camera" is a reserved word in the Unity libraries, which means you can't use it for variable names. Instead try something like "theCamera" or "playerCamera".
~
Secondly my guess at what's not working would be a child transform issue. When you have an object nested under another object it gets it own coordinate system separate from the world coordinates. What this means is that 0,0,0 in world space, and 0,0,0 in a nested objects space might not be the same position. To fix this just make sure both the player and the camera are not children of any other object.
~
Failing that fix, let me know and I'll see if I can brainstorm some more. Overall your code should work.
Camera or player are not a child of anything. Just separate game objects.
And I just deleted the camera and placed another in its place and it is working normally. Strange.
Unity can be weird like that sometimes. Good luck with your game!