currently i have a script that causes an object to turn and move towards the cursor, however it instantly turns it. I want the script to slowly turn towards the cursor. currently the script i am using is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermovement : MonoBehaviour
{
[SerializeField] float movementspeed = 150f;
[SerializeField] float turnspeed = 309f;
void Update()
{
Turn();
Thrust();
}
void Turn()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane plane = new Plane(Vector3.up, Vector3.zero);
float distance;
if (plane.Raycast(ray, out distance))
{
Vector3 target = ray.GetPoint(distance);
Vector3 direction = target - transform.position;
float rotation = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, rotation, 0);
}
}
void Thrust()
{
if (Input.GetAxis("Vertical") > 0)
transform.position += transform.forward * movementspeed * Time.deltaTime * Input.GetAxis("Vertical");
}
}
Haven’t tested:
if (plane.Raycast(ray, out distance))
{
Vector3 target = ray.GetPoint(distance);
Vector3 direction = target - transform.position;
float rotation = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
transform.rotation = Quaternion.RotateTowards( transform.rotation, Quaternion.Euler(0, rotation, 0), Time.deltaTime * 1 ); // Replace `1` by the value you want, or even by a public variable
}
// damp level
float rotatetime;
transform.rotation = Quaternion.Euler(0, mathf.Lerp(tranform.rotation.y, rotation, rotatetime), 0);
will this work?