Question by
BloatedNikNak · Oct 31, 2018 at 08:16 PM ·
rotationraycastplayer movementmouseposition
Character rotation, only happens when mouse is rotated around world centre (0,0,0)
Character rotation, only happens when mouse is rotated around world centre (0,0,0) -
The MoveToMouse() works perfectly, click on a point in the world, and the player moves, and camera follows.
But when holding shift to rotate the character to where mouse is pointing, it only points relative to the world centre.
using UnityEngine;
using UnityEngine.AI;
public class ClickToMove : MonoBehaviour
{
NavMeshAgent player;
public float rotSpeed = 10f;
void Start()
{
player = GetComponent<NavMeshAgent>();
}
void Update()
{
MoveToMouse();
LookAtMouse();
}
void MoveToMouse()
{
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
{
player.destination = hit.point;
}
}
}
void LookAtMouse()
{
if (Input.GetKey(KeyCode.LeftShift))
{
RaycastHit lookHit;
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out lookHit, 100);
//transform.LookAt(lookHit.point);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookHit.point), rotSpeed * Time.deltaTime);
}
}
}
Comment