Ok, so im very new to Csharp. But i thought it’d be cool to learn how to make diablolike click to move movement scripts. Well im stuck. everything works as intended except the character spins in place when standing still. Help me unity forum. you’re my only hope. what am i doing wrong?
using UnityEngine;
using System.Collections;
public class Movement_PC : MonoBehaviour
{
public float speed;
private Vector3 position;
public CharacterController cc;
void Start ()
{
}
void Update()
{
if (Input.GetMouseButton(0))
{
locatePostion();
}
moveToPosition();
}
void locatePostion()
{
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 + 1f, hit.point.z);
Debug.Log(position);
}
}
void moveToPosition()
{
if (Vector3.Distance(transform.position, position) > 1);
{
Quaternion newRotation = Quaternion.LookRotation(position - transform.position);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
cc.SimpleMove(transform.forward * speed);
}
}
}