- Home /
Follow Camera 3D Player
I have camera follow script. But when I attached this script to player, camera doesnt follow the camera. Help me please :) thank you
Regards, Tio.
public Camera mainCamera;
void Start(){
mainCamera = GameObject.FindWithTag ("MainCamera").camera;
}
void Update () {
mainCamera.transform.position = new Vector3
(transform.position.x, 1, -1);
}
I've been try it, but it still not followed the player.
This is my player script. When I press UpArrow I want player move, play animation and camera will follow the player. But its still not working.
public float $$anonymous$$oveSpeed = 5.0f;
void Update () {
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow)) {
animation.Play ("run");
transform.position += transform.forward * $$anonymous$$oveSpeed * Time.deltaTime;
} else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space)) {
animation.Play ("jump");
transform.position += transform.forward * $$anonymous$$oveSpeed * Time.deltaTime;
} else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow)) {
animation.Play ("right");
transform.position += transform.right * $$anonymous$$oveSpeed * Time.deltaTime;
} else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow)) {
animation.Play ("left");
}
}
Answer by liortal · Apr 25, 2014 at 02:35 PM
I am not sure what you meant to do. In many cases, it is required that the camera will follow the player.
Here's a sample script (actually taken from Unity's 2D project sample):
using UnityEngine;
using System.Collections;
public class FollowPlayer : MonoBehaviour
{
private Transform player; // Reference to the player.
void Awake ()
{
// Setting up the reference.
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update ()
{
// Set the position to the player's position with the offset.
transform.position = player.position;
}
}
This, when attached to the camera, will follow the player's movement.
Answer by socialspiel · Apr 25, 2014 at 01:41 PM
Use LateUpdate for this. LateUpdate is called after all Update functions on alle GameObjects have been called. So you make sure, that the camera is updated after all other movement has occured.
Furthermore check if you got the reference for the camera right. You could for easier access just use Camera.main for the first active camera.
void LateUpdate () {
Camera.main.transform.position = new Vector3
(transform.position.x, 1, -1);
}
It may be a coding style preference, but i tend to like more code that modifies the camera's properties (transform) on the camera object, than having a script that modifies the camera's properties from another game object (the player).
in my code the script that controls the camera is attached to the camera. the op attaches the camera control script to the player and asked what might be wrong with his code. hence my answer.
Your answer
Follow this Question
Related Questions
Camera follow smoothness problem 1 Answer
camera follow an game object 0 Answers
2d camera follow player by looking ahead. 1 Answer
Camera Border Follow 1 Answer