- Home /
Make a camera move towards player when the player moves out of the camera's vision
I am making a top down/bird perspective game.
My player character can move around and I can turn the player around 360.
But what I am missing is a way for the camera to follow the Player.
I know that I can make my camera the child of the player character so it follows it but that makes my player spin uncontrolled because of the way the rotation works with the camera.
Script:
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
Rigidbody rigidBody;
public float speed = 4;
Vector3 lookPos;
void Start ()
{
rigidBody = GetComponent<Rigidbody>();
}
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100))
{
lookPos = hit.point;
}
Vector3 lookDir = lookPos - transform.position;
lookDir.y = 0;
transform.LookAt (transform.position + lookDir, Vector3.up);
}
void FixedUpdate ()
{
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (horizontal, 0, vertical);
rigidBody.AddForce (movement * speed / Time.deltaTime);
}
}
I need a solution:
Make the camera move as the player gets out of its vison.
New player movement script
Answer by Garazbolg · Nov 05, 2015 at 02:32 PM
For the Camera following script you can set a script on the camera to folow the Player like this :
public class CameraFollow : MonoBehaviour
{
public Transform target;
public float speed = 0.7f;
public void Start(){
target = GameObject.FindGameObjectsWithTag("Player")[0].transform;
}
public void Update(){
transform.position = Vector3.Lerp(transform.position,new Vector3(target.position.x,transform.position.y,target.position.z),speed * Time.deltaTime);
}
}
You can change the speed value to suit your needs. This script is pretty basic i think i saw it on the first Unity Tutorial I've seen.
For the other i didn't understand what you were saying, could you be more clear.
I tough the script would be good in the beginning, It was. But now I have a few problems actually: IndexOutOfRangeException: Array index is out of range. CameraFollowScript.Start () (at Assets/Player Controller/CameraFollowScript.cs:10)
I get that error, and also the camera movement is kind of buggy in the sense that it does not go smooth. I can see it on my flashlight of the player.
Yeah i forgot to say that : you need to set the tag on your player GameObject to "Player" if you don't it won't work, and if you don't have a Player at the moment of the Camera Start it won't work.
A cleaner way to do :
target = GameObject.FindGameObjectsWithTag("Player")[0].transform;
would be to do :
GameObject player = GameObject.FindWithTag("Player");
if(player)
target = player.transform;
and add in your update some way to check if there is a target :
public void Update(){
if(target)
transform.position = Vector3.Lerp(transform.position,new Vector3(target.position.x,transform.position.y,target.position.z),speed * Time.deltaTime);
}
But then if you don't have a Player at the start your Script won't do anything, unless you set the target attribute of CameraFollow somewhere else (in the start of your Player Script for exemple)
void Start ()
{
rigidBody = GetComponent<Rigidbody>();
CameraFollow camera = Camera.main.GetComponent<CameraFollow>();
if(camera)
camera.target = transform;
}
I did the previous script quickly but you may want to look at the documentation of Unity and some manuals to understand how it works.
Answer by DeathDev · Nov 06, 2015 at 12:06 AM
This should work, let me know if you have any problems. :)
PS - If you don't want to wait for the character to leave the screen before the camera moves, simply remove the if statement inside of the Update function. Also if you need a more complex version(for instance one that actually makes sure that the entire player is inside of the bounds, let me know!)
using UnityEngine; using System.Collections;
public class CameraMovement : MonoBehaviour
{
public Transform playerObject;
public float moveSpeed = 1;
public bool keepHeight = true; //If we should keep the current height.
public bool dispArea = true; //If we should display the cam area.
public float buffer = 20; //The area(in pixels) around the edges that'll cause the camera to move.
private Vector3 _pos; //The cached pos.
private Rect _moveArea; //If the obj exits this area, we'll move to catch up.
//Setting the playerObject to the scene's player object, along with caching the height.
void Start()
{
playerObject = GameObject.FindGameObjectWithTag("Player").transform;
_pos = transform.position;
//Setting our camera area.
_moveArea.x = ((Screen.width / 2) - buffer / 2);
_moveArea.width = buffer;
_moveArea.y = ((Screen.height / 2) - buffer / 2);
_moveArea.height = buffer;
}
void Update()
{
if (!vecInArea(playerObject.position))
{
Vector3 _newPos = playerObject.position;
if (keepHeight)
_newPos.y = _pos.y;
transform.position = Vector3.Lerp(transform.position, _newPos, moveSpeed * Time.deltaTime);
}
}
bool vecInArea(Vector3 pos)
{
Vector2 screenPos = Camera.main.WorldToScreenPoint(pos);
return _moveArea.Contains(screenPos);
}
void OnGUI() { if (dispArea) GUI.color = new Color(1.0f, 1.0f, 1.0f, 0.25f); GUI.Box(_moveArea, ""); }
}
As Garazbolg said, more information is needed for the second point.
Your answer
Follow this Question
Related Questions
Smooth Camera Movement script with problem. 0 Answers
Change character's Y rotation based on velocity. 1 Answer
Camera Zoom script stops ability to pan 1 Answer
Camera move with mouse 0 Answers