- Home /
How can I limit the rotational speed of a gameObject tracking my mouse?
As my first project I have settled on a space shooter. I have my shuttle tracking my mouse position and moving in that direction (x,y,z). I'm pleased with how the ship advances in the direction (x,y) of the mouse, however, I find it ugly that when I move my mouse from the top of the screen to the bottom of the screen, the ship instantly flips to follow the angle (y).
I'd like to set a limit on how fast my ship rotates on the Z axis but have had no luck trying to implement it in my code. All the answers I see relate to transorm.Rotate and use speed * Time.deltaTime to achieve this.
Is there any way to do the same thing for what I already have? Or am I better off scrapping this code and starting again?
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour {
 
     public float PlayerShipSpeed = 10f; //Set maximum travel speed for player ship
 
     void Start () {
     }
 
     void Update () {
         var mouse = Input.mousePosition;
         var screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
         var offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y);
         var angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0, 0, angle - 90f);
         var targetPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         targetPos.z = transform.position.z;
         transform.position = Vector3.MoveTowards (transform.position, targetPos, PlayerShipSpeed * Time.deltaTime);
     }
 }
Thanks for taking the time to answer a noob :)
Answer by xortrox · May 03, 2018 at 05:35 PM
Untested code. Read up on this, or Quaternion in general.
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class PlayerMovement : MonoBehaviour {
  
      public float PlayerShipSpeed = 10f; //Set maximum travel speed for player ship
  
      public float DegreesPerSecond = 180.0f;
 
      void Start () {
      }
  
      void Update () {
          var mouse = Input.mousePosition;
          var screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
          var offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y);
          var angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
          Quaternion targetRotation = Quaternion.Euler(0, 0, angle - 90f);
          transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, DegreesPerSecond * Time.deltaTime);
          var targetPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
          targetPos.z = transform.position.z;
          transform.position = Vector3.MoveTowards (transform.position, targetPos, PlayerShipSpeed * Time.deltaTime);
      }
  }
I've moved on to another script in order to add a mouse deadzone when over the space ship, and a projectile. I still have the issue though. I'll try this, and report back. Thanks
You could additionally check for deadzone using your existing screenPoint variable
   using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
   
   public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
   
       public float PlayerShipSpeed = 10f; //Set maximum travel speed for player ship
   
       public float DegreesPerSecond = 180.0f;
 
       // Assu$$anonymous$$g pixels as unit
       public float RequiredCursorDistanceFromShip = 200.0f;
 
       void Start () {
       }
   
       void Update () {
           var mouse = Input.mousePosition;
           var screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
 
           if(Vector2.distance(screenPoint, new Vector2(mouse.x, mouse.y)) < RequiredCursorDistanceFromShip) 
           {
                // Stop the update here if we are inside the ship deadzone.
                return;
           }
 
           var offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y);
           var angle = $$anonymous$$athf.Atan2(offset.y, offset.x) * $$anonymous$$athf.Rad2Deg;
           Quaternion targetRotation = Quaternion.Euler(0, 0, angle - 90f);
           transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, DegreesPerSecond * Time.deltaTime);
           var targetPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
           targetPos.z = transform.position.z;
           transform.position = Vector3.$$anonymous$$oveTowards (transform.position, targetPos, PlayerShipSpeed * Time.deltaTime);
       }
   }
Again, untested code.
Can I show you what I have? For critique? Only if you have have a moment to spare. It's not very long. I'm only doing simple stuff but there's a learning curve for sure. I can paste it in an edit later on.
Your answer
 
 
             Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Pysics not working as expected 1 Answer
Joystick move Left Right and down, plus rotation. 0 Answers
How do i determin if an objects rotation is between 2 values on each axis? 0 Answers
C# Water Rotation 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                