- Home /
Rotate gameobject to face mouse click over time
I am completely new to Unity but haven't found an answer to what i am trying to do.
In essense i am trying to click at a point around my model ship and have it rotate over time to face that location. It is a 3D model that stays on the horizontal plane, but for ease of explaining the image would be a top dpwn view. Mouse click red x and nose rotates to face. Think of it like a ship on the sea.
I wrote the below but that seems to rotate the model around the world centre than its own Y axis. Also i have only managed to perform something on mouse down rather than click, but can capture the mouse click world position to the console.
For clarity eventually i want to have the speed of the rotation affected by the forward speed of the ship. If stopped the speed of rotation would be slow. 25% forward speed would be optimal turn rate and increases above this would be slower. I don't need that answering, as i would like to try and have a go at figuring it out myself, but in case it affects the turn question.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MoveToMouse : MonoBehaviour
{
public Camera cam;
public Transform obj;
public float speed = 1.0f;
public Vector3 worldPosition;
void Update()
{
if (!Input.GetMouseButton(0))
return;
Plane plane = new Plane(Vector3.up, 0);
float distance;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
worldPosition = ray.GetPoint(distance);
Debug.Log(worldPosition.ToString("F4"));
// Determine which direction to rotate towards
Vector3 targetDirection = worldPosition - transform.position;
// The step size is equal to speed times frame time.
float singleStep = speed * Time.deltaTime;
// Rotate the forward vector towards the target direction by one step
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
// Draw a ray pointing at our target in
Debug.DrawRay(transform.position, newDirection, Color.red);
// Calculate a rotation a step closer to the target and applies rotation to this object
transform.rotation = Quaternion.LookRotation(newDirection);
}
}
}
Answer by Inkarr · May 08, 2021 at 09:44 AM
I managed to figure out the below code did what i required. Probably not the prettiest code in the world, but it's my first stab at Unity so be gentle.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipMovement : MonoBehaviour
{
// CaptureRightMouseClickPos Params
public Vector3 RightMouseClickPosition;
private float range = 0.0f;
// AngleToMouse Params
public float AngleToMouseClick;
// ShipRotation Params
public float StartY;
public float CurrentY;
public float FinalY;
public float RotationSpeed = 5.0f;
void OnGUI()
{
CaptureRightMouseClickPos();
AngleToMouse();
ShipRotation();
}
// Capture the mouseclick position
void CaptureRightMouseClickPos()
{
range = range + Time.deltaTime;
// activate on right mouse click
if (Input.GetMouseButtonUp(1))
{
Plane plane = new Plane(Vector3.up, 0);
float distance;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
RightMouseClickPosition = ray.GetPoint(distance);
}
}
}
// Capture the angle from forward to the mouse click
void AngleToMouse()
{
if (Input.GetMouseButtonUp(1))
{
Vector3 targetDir = RightMouseClickPosition - transform.position;
Vector3 forward = transform.forward;
AngleToMouseClick = Vector3.SignedAngle(targetDir, forward, Vector3.down);
Vector3 eulerAngles = transform.rotation.eulerAngles;
}
}
// Apply the angle and rotate the GameObject at a constant speed
void ShipRotation()
{
if (Input.GetMouseButtonUp(1))
{
StartY = transform.eulerAngles.y;
}
CurrentY = transform.eulerAngles.y;
FinalY = StartY + AngleToMouseClick;
float angle = Mathf.MoveTowardsAngle(CurrentY, FinalY, RotationSpeed * Time.deltaTime);
transform.eulerAngles = new Vector3(0, angle, 0);
}
}
Your answer
Follow this Question
Related Questions
How to create gameobjects by clicking in the scene view in the editor? 1 Answer
ScreenToWorldPoint and Mouse Position 2 Answers
Rigidbody Disable Velocity/Movement? 1 Answer
Character Rotation Now Matching GameObject's Local Rotation 0 Answers
Mouse click on a game object to animate the main camera. 0 Answers