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:
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.
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.