void Update()
{
//player faces mouse
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
float hitDist = 0.0f;
if (playerPlane.Raycast(ray, out hitDist))
{
Vector3 targetPoint = ray.GetPoint(hitDist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
targetRotation.x = 0;
targetRotation.z = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 10f * Time.deltaTime);
}
So I am trying to rotate my player object toward the mouse. This script does work but the player rotates in a big circle on the x and z axis. I want my player to turn around but instead he is walking a big circle to turn around. This seemed like a good example to work with because the player won’t just spin around like a top if you have to turn.
If it help… this also moves in too big of a circle
Vector3 mousePos = UnityEngine.Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.Euler(0, Input.mousePosition.y, 0);
Just in case someone needed my older answer I left it below. Here is my new solution so far. Rotate slowly to target position? - Questions & Answers - Unity Discussions
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundplane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if (groundplane.Raycast(cameraRay, out rayLength))
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit;
if(Physics.Raycast(camRay, out floorHit)) {
Vector3 playerToMouse = floorHit.point - transform.position;
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
transform.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, Time.deltaTime * turnSpeed);
}
}
Old answer below :
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private Rigidbody myRigidBody;
private Vector3 moveInput;
private Vector3 moveVelocity;
private Camera mainCamera;
// Use this for initialization
void Start () {
myRigidBody = GetComponent<Rigidbody>();
mainCamera = FindObjectOfType<Camera>();
}
// Update is called once per frame
void Update () {
moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
moveVelocity = moveInput * moveSpeed;
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundplane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if (groundplane.Raycast(cameraRay, out rayLength))
{
Vector3 pointToLookAt = cameraRay.GetPoint(rayLength);
Debug.DrawLine(cameraRay.origin, pointToLookAt, Color.green);
transform.LookAt(pointToLookAt);
}
}
//something like every 50ms
void FixedUpdate() {
myRigidBody.velocity = moveVelocity;
}
}
Ok so this is working. I got it from the tutorial Unity Top Down Shooter #1 - Player Movement & Look - YouTube. I have no idea what went wrong with that other script. Maybe it was just Using Slerp instead of lookat. I’ll update this if I find a way to make the character move a bit while turning