The question is answered, right answer was accepted
Trying to get Player Character to look at the mouse pointer... which i have done, but would like to smooth the rotation.
I'm wondering if someone would be so kind to point me in the right direction... I have this code, which i'm using in an isometric game. My aim is to get the Character to look at the mouse pointer... which i have done, but would like to smooth the rotation.
I understand from reading the Docs that it is not possible using my existing code ie. transform.LookAt();
and have read that i should use Quaternions - but when i try to do this I get a complier error, saying i can not convert from 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'
I have no idea if i am approaching this in the right way, I had the idea of using similar code for what i used for my movement, to detect the mouse location.
I'm not looking for someone to feed me the answer, just a hint maybe, on how to convert the above or if there is a different keyword i should be looking for.
FYI: This is being called in Update() - and the script is on the character
using UnityEngine;
public class ClickToMove : MonoBehaviour
{
public float rotSpeed = 10f;
void Update()
{
LookAtMouse();
}
private 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, lookHit.point, rotSpeed * Time.deltaTime);
}
}
}
Answer by linh60bpm · Oct 29, 2018 at 09:46 PM
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(lookHit.point), rotSpeed * Time.deltaTime);
//Quaternion is different from Vector3, so you must use that function
//You can also use LookAt() function, just use another Vector3 like
Vector3 lookAt = new Vector3 (hit.point.x, transform.position.y, hit.point.z);
transform.LookAt(lookAt);
The first part of your answer does precisely what i was looking for. Thank you for taking the time to help :)
Follow this Question
Related Questions
How to rotate a camera slowly along a tween? 1 Answer
Grabbing the Relative eulerAngles.y of a Rotation 1 Answer
Smooth 90 degree rotation on keypress 3 Answers
Clamp a rotation in unity 0 Answers
-= Time.deltaTime 1 Answer