Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by beginnerOlle · Dec 13, 2013 at 04:24 PM · cameratransformrigidbody2d

Moving camera with player in 2d

Hi

I'm new to unity and I'm trying to write a script for my camera move my it with the player object. Hoever, I found I cannot use the "transform.player" because unity can't recognize player as a Ridgidbody2D with position for some reason. Someone might see how I solve this, i.e. getting hold of the player position?

 using UnityEngine;
 using System.Collections;
 
 public class Camerafollow : MonoBehaviour
 {
 
 
 
 
         // Use this for initialization
         void Start ()
         {
     
         }
     
         // Update is called once per frame
         void Update ()
         {
         Rigidbody2D player = Instantiate(GameObject.Find ("Player")) as Rigidbody2D;
         Vector2 hastighet = player.velocity;
         if (hastighet !=  0) {
             Vector2 playerpos;
             playerpos.x = transform.player;
             playerpos.y = transform.player;
             transform.position = playerpos;
                 }
                 
         }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Dec 13, 2013 at 04:43 PM

Instantiate() creates a new player, so your code is creating a new player every frame. What you want to do is get a reference to the player in Start(), then use that reference to position the camera. Something like:

 using UnityEngine;
 using System.Collections;
 
 public class Camerafollow : MonoBehaviour {
 
     private Transform player;
 
     void Start () {
         player = GameObject.Find ("Player").transform;
     }
     
     void Update () {
         Vector3 playerpos = player.position;
         playerpos.z = transform.position.z;
         transform.position = playerpos;
     }
 }
Comment
Add comment · Show 4 · 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 beginnerOlle · Dec 14, 2013 at 07:58 AM 0
Share

Thanks, that helped alot.

avatar image rizzlinux1388 · Jul 03, 2014 at 11:54 PM 0
Share

Thank you that worked perfectly. I noticed the behavior seemed to be the same when the camera is a child to the player object. Is that correct?

avatar image videoanime · Oct 18, 2014 at 06:58 AM 0
Share

I'm close to do what I want, your example helps me a lot, but imagine this, I want that the camera follows my character at the same height, I mean, the same coordinate "y" and only when my character has arrived at the middle of the screen, then, the unique coordinate I want to move is "x", if I apply the example you put, the screen goes down and it starts to move since the beginning of the program.

I tried to assign values to the coordinate "x" in this way:

 void LateUpdate ()
     {
     Vector3 Finnpos = Finn.position;
     Finnpos.z = transform.position.z;
     if (Finnpos.x >= transform.position.x)
         transform.position.x = Finnpos.x;
     //transform.position = Finnpos;
     }

But unity says: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

Can you help me?

avatar image robertbu · Oct 18, 2014 at 07:01 AM 0
Share

You should ask this as a new question, not as a 'Solution' to an existing question. As for the error, you need to do:

  if (Finnpos.x >= transform.position.x)
      Vector3 v = transform.position;
      v.x = Finnpos.x;
      transform.position = v;
  }

But this snipped does not take a look at the larger tracking issue, only solves the compiler error.

avatar image
2

Answer by Laurindo · Apr 26, 2015 at 08:48 PM

Tested and worked!!! Sorry for my english.

1 - Create an Game Object with name "Player"

2 - Create an Game Object with name "Boundary Markers"

    • Create a game object with name "Left Marker" into Boundary Markers

    • Create a game object with name "Right Marker" (and change a Transform position x for 25 Right Markers's ) into Boundary Markers

3 - Create script "Camera Follow"

using UnityEngine; using System.Collections;

**public class CameraFollow : MonoBehaviour {

 public Transform player;
 public Transform farLeft;
 public Transform farRight;
 void Update () {
 
     Vector3 newPosition = transform.position;
     newPosition.x = player.position.x;
     newPosition.x = Mathf.Clamp(newPosition.x, farLeft.position.x, farRight.position.x);
     transform.position = newPosition;
 }

}**

4 - Attached Game Object Player on player

5 - Attached Game Object Left Marker on farLeft

6 - Attached Game Object Right Marker on farRight

For more informations: http://unity3d.com/pt/learn/tutorials/modules/beginner/live-training-archive/making-angry-birds-style-game-pt2

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Howto set main.camera as a parent when main.camera itself is a child of a prefab 1 Answer

nullifying targetObject after a Smooth LookAt Transition 2 Answers

Move JoyStick Together With Camera 1 Answer

Camera stutter when following a RigidBody2D 0 Answers

Camera distance from mesh not center 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges