Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Leonid198 · Feb 28, 2019 at 02:24 AM · 2dtransformposition

How do I make my camera follow the player without rotating in 2D?

Here's what I got so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraFollow : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
 
         // camera = GetComponent<Transform> ();
         player = GameObject.Find ("Player");
 
     }
 
     // Update is called once per frame
     void Update () {
         
         GetComponent<Transform>.postion.x = player.postion.x;
         GetComponent<Transform>.postion.y = player.postion.y;
         GetComponent<Transform>.postion.z = player.postion.z - 10;
 
     }
 }
 
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
0

Answer by Lynkfox · Feb 28, 2019 at 05:07 AM

so, you want your camera to follow behind your character, but without turning when you turn? Like a 3rd person adventure game view (where the camera is always facing the same direction, no matter how the player moves around)

Instead of a script to move your camera, just put your camera as a child object of the player object. It will follow at the distance you set it to in the editor, and stay there. You can keep the camera always looking at the player object with a script if you need to, but if I recall correctly, you won't need that.

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
avatar image
0

Answer by WarmedxMints · Feb 28, 2019 at 07:55 AM

You shouldn't use GetComponent in a method which runs every frame. It is slow and can hit performance when called every frame. You were correct to get a reference to the camera transform in the Start method so I am not sure why you commented that out.

I also personally do not like string lookups such as GameObject.Find("Player"); The issue you can have is they will never give you any compiler errors and much be 100% correct in case and spelling. A prefered method to find the player would be using FindObjectOfType().

That being said, here is a simple camera follow script I made some time ago which may be of help to you;

 using UnityEngine;
 
 public class SimpleCameraFollow : MonoBehaviour
 {
     public enum FollowType
     {
         Rigid,
         Lerp,
         Slerp,
     }
 
     //Populate in Inspector with you players transform
     public Transform    TargetToFollow;
 
     //The distance the camera will be from the player
     public Vector3      FollowOffset        = new Vector3(0, 0, -10);
 
     //How quickly the camera moves
     public float        FollowSpeed         = 5f;
 
     //How the camera will follow
     public FollowType   FollowMethod        = FollowType.Lerp;
 
     private Transform   _cameraTransform;
     private Vector3     _targetPos;
 
     private void Start()
     {
         if(TargetToFollow == null)
             Debug.LogError($"{nameof(TargetToFollow)} is null", this);
 
         _cameraTransform = transform;
     }
 
     private void LateUpdate()
     {
         switch (FollowMethod)
         {
             case FollowType.Rigid:
                 _targetPos = TargetToFollow.position + FollowOffset;
                 break;
             case FollowType.Lerp:
                 _targetPos = Vector3.Lerp(_cameraTransform.position , TargetToFollow.position + FollowOffset, Time.deltaTime * FollowSpeed);
                 break;
             case FollowType.Slerp:
                 _targetPos = Vector3.Slerp(_cameraTransform.position, TargetToFollow.position + FollowOffset, Time.deltaTime * FollowSpeed);
                 break;
         }
 
         _cameraTransform.position = _targetPos;
     }
 }
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 Leonid198 · Feb 28, 2019 at 08:48 PM 0
Share

I will try this soon, Unity 5 compiler keeps updating this part in = signs:

    void Update () {
          
          ==GetComponent<Transform>==.postion.x = player.postion.x;
          ==GetComponent<Transform>==.postion.y = player.postion.y;
          ==GetComponent<Transform>==.postion.z = player.postion.z - 10;
  
      }

it was "camera" I try to run it and it places that in there because Unity.

avatar image WarmedxMints Leonid198 · Feb 28, 2019 at 08:52 PM 0
Share

GetComponent().position would be the correct way of using the GetComponent function. However, there is no need for it as you can just do transform.position.

Although, if you are modifying the transform every frame, it isn't a bad idea to cache it as I have done in the above script.

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

231 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 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 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

How Do I Align Game Objects Evenly In 2D Space? 0 Answers

An easy way to get the global position from a child object 5 Answers

How to make your object follow your touch position faster or instantaneously??? 2 Answers

Drawing a (debug) line between anchored UI element and mouse? 0 Answers

Convert Input.mousePosition to RectTransform pivot position 2 Answers


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