I need some help people, any would be appreciated! Currently I’ve got a Diablo style RPG project, how ever I want the camera to rotate around the player whilst he’s moving instead of staying static the whole time. I’ve looked through guides but nothing I’ve found has solved my issue.
Here’s the script, if anyone knows how to help then please comment or PM me ![]()
using UnityEngine;
using System.Collections;
public class ClickToMove : MonoBehaviour
{
public float speed;
public CharacterController controller;
private Vector3 position;
public AnimationClip run;
public AnimationClip idle;
public static bool attack;
public static bool die;
// Use this for initialization
void Start ()
{
position = transform.position;
}
// Update is called once per frame
void Update ()
{
if (!attack&&!die)
{
if (Input.GetMouseButton (0))
{
//Locate where the player clicked on the terrain
locatePosition ();
}
//Move player to position
moveToPosition ();
}
else
{
}
}
void locatePosition()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray, out hit, 1000))
{
if(hit.collider.tag!="Player"&&hit.collider.tag!="Enemy")
{
position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
}
}
}
void moveToPosition()
{
if (Vector3.Distance (transform.position, position) > 1) {
Quaternion newRotation = Quaternion.LookRotation (position - transform.position, Vector3.forward);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * 10);
controller.SimpleMove (transform.forward * speed);
GetComponent<Animation>().CrossFade (run.name);
}
//Game Object not moving
else
{
GetComponent<Animation>().CrossFade (idle.name);
}
}
}
As you can see the script allows me to attack, die and click on the screen to move - surely there can be some line of code or something I can alter to have the camera rotate the player?
PS happy easter ![]()