Im making a top down RPG and dont have much experience with scripts, i have 2 Scripts so far one for moving(click to move) and a camera follow script.
This is the click to move script
using UnityEngine;
using System.Collections;
public class moveToClikc : MonoBehaviour {
public float speed;
public CharacterController controller;
private Vector3 position;
// Use this for initialization
void Start ()
{
position = transform.position;
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(0))
{
locatePosition();
}
moveToPosition();
}
void locatePosition()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, 1000))
{
position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
}
}
void moveToPosition()
{
if (Vector3.Distance(transform.position, position)>2)
{
Quaternion newRotation = Quaternion.LookRotation(position - transform.position);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.fixedDeltaTime = 10);
controller.SimpleMove(transform.forward * speed);
}
}
}
And here is the camera script
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour {
public Transform target;
public float smoothing = 5f;
Vector3 offset;
void Start ()
{
offset = transform.position - target.position;
}
void FixedUpdate ()
{
Vector3 targetCamPos = target.position + offset;
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
When I use the Camera script with a simple move script(move with WASD) it works, but when i use the Click to move script the camera script doesn’t work