Unity Drawing From Mouseclick to Plane
- Spaces
- Default
- Assist Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Home /
0
Rotate gameobject to confront mouse click over fourth dimension
I am completely new to Unity but oasis't found an answer to what i am trying to do.
In essense i am trying to click at a indicate 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, only for ease of explaining the paradigm would be a top dpwn view. Mouse click carmine 10 and nose rotates to face. Think of information technology 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 tin capture the mouse click globe position to the panel.
For clarity eventually i desire to have the speed of the rotation affected by the forward speed of the transport. If stopped the speed of rotation would be deadening. 25% forward speed would exist optimal plow charge per unit and increases above this would be slower. I don't demand that answering, as i would similar to attempt and have a go at figuring it out myself, merely in instance information technology affects the plow question.
using System.Collections; using Arrangement.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class MoveToMouse : MonoBehaviour { public Camera cam; public Transform obj; public float speed = ane.0f; public Vector3 worldPosition; void Update() { if (!Input.GetMouseButton(0)) return; Plane plane = new Plane(Vector3.upwardly, 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 past one stride Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f); // Depict a ray pointing at our target in Debug.DrawRay(transform.position, newDirection, Colour.reddish); // Calculate a rotation a step closer to the target and applies rotation to this object transform.rotation = Quaternion.LookRotation(newDirection); } } } one Respond
· Add your reply
- Sort:
0
Answer past Inkarr · May 08, 2021 at 09:44 AM
I managed to figure out the below lawmaking did what i required. Probably not the prettiest code in the world, but it'south my commencement stab at Unity so exist gentle.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ShipMovement : MonoBehaviour { // CaptureRightMouseClickPos Params public Vector3 RightMouseClickPosition; individual float range = 0.0f; // AngleToMouse Params public bladder AngleToMouseClick; // ShipRotation Params public bladder StartY; public bladder CurrentY; public float FinalY; public bladder 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(ane)) { Plane aeroplane = new Aeroplane(Vector3.up, 0); bladder distance; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (plane.Raycast(ray, out distance)) { RightMouseClickPosition = ray.GetPoint(altitude); } } } // Capture the angle from forrad to the mouse click void AngleToMouse() { if (Input.GetMouseButtonUp(1)) { Vector3 targetDir = RightMouseClickPosition - transform.position; Vector3 forward = transform.forward; AngleToMouseClick = Vector3.SignedAngle(targetDir, forrard, Vector3.downwardly); 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; bladder bending = Mathf.MoveTowardsAngle(CurrentY, FinalY, RotationSpeed * Fourth dimension.deltaTime); transform.eulerAngles = new Vector3(0, angle, 0); } } Your respond
Welcome to Unity Answers
The best place to inquire and answer questions about development with Unity.
To assist users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to cheque out our Noesis Base for unremarkably asked Unity questions.
If you are a moderator, run into our Moderator Guidelines page.
Nosotros are making improvements to UA, come across the list of changes.
Follow this Question
Related Questions
barreragramemptere1996.blogspot.com
Source: https://answers.unity.com/questions/1826993/rotate-gameobject-to-face-mouse-click-over-time.html
0 Response to "Unity Drawing From Mouseclick to Plane"
Postar um comentário