- Home /
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;
}
}
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.
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;
}
}
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.
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
Follow this Question
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