Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Inkarr · Apr 04, 2021 at 04:46 PM · rotationgameobjectmousepositionmouseclick

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.

alt text

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);
         }
     }
 }


rotate.png (2.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

203 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges