• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_Z70jaPTIIp7DuQ · Jan 19, 2018 at 05:02 PM · camera-movementtransform.positionfollow player

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_Z70jaPTIIp7DuQ · Jan 19, 2018 at 05:35 PM 0
Share

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.

avatar image spidermancy612 unity_Z70jaPTIIp7DuQ · Jan 19, 2018 at 06:35 PM 0
Share

Unity can be weird like that sometimes. Good luck with your game!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help updating camera position to go above the player 2 Answers

How do I get camera movement through controller input. 1 Answer

Follow Player in 2D Game 2 Answers

camera problem 0 Answers

Move camera when mouse is near the edges of the screen 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges