• 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 LeftyTwoGuns · Aug 05, 2013 at 12:00 AM · mouserotateaim

Rotating object with mouse

I have simple turret on a 2D plane, whose child barrels rotate on its Z axis by 30-150 degrees. At first I had them rotating using J and K keys just for testing and it worked perfectly but I've switched it to rotate moving the mouse and I can't figure out how to rotate it based on just moving the mouse back and forth on the X axis. Here's my script:

 using UnityEngine;
 using System.Collections;
 
 public class Turret : MonoBehaviour {
     
         //Player Turret
     public GameObject barrels;
     public float rotateSpeed = 5;
     
     public float minAngle = 60;
     public float maxAngle = 280;
 
 
     void Update () {
         //Turret rotation and angle clamp
     if(Input.GetAxis("Mouse X") < 0) barrels.transform.Rotate(0, 0, rotateSpeed * Time.deltaTime);
     if(Input.GetAxis("Mouse Y") > 0) barrels.transform.Rotate(0, 0, -rotateSpeed * Time.deltaTime);
     transform.eulerAngles = new Vector3(0, 0, Mathf.Clamp (transform.eulerAngles.z, minAngle, maxAngle));
         
     }
 }


Obviously, moving the mouse on the Y axis isn't the right way to go, but like I said I can't figure out how to get the barrels to rotate fully moving the mouse just on the X-axis. Thanks in advance

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by LeftyTwoGuns · Aug 13, 2013 at 12:30 AM

Well, I went back to my original script and it seems the solution was there all a long:

     if(Input.GetAxis("Mouse X") < 0) {
             barrels.transform.Rotate(0, 0, rotateSpeed * Time.deltaTime);
         }
     if(Input.GetAxis("Mouse X") > 0) {
             barrels.transform.Rotate(0, 0, -rotateSpeed * Time.deltaTime);
         }
             
     transform.eulerAngles = new Vector3(0, 0, Mathf.Clamp (transform.eulerAngles.z, minAngle, maxAngle));

But I'm not really sure how it's working even though the middle of the screen is never defined in the script. At any rate, thank you for all the suggestions! I've learned quite a bit just trying to figure out something so simple!

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
avatar image
0

Answer by dorpeleg · Aug 05, 2013 at 10:39 AM

Here is your problem.

The x/y axis of the mouse are diffrent from the x/y axis of the scene/gameobject.

The 0,0 of the mouse, is the bottom left corner of the screen.

so doing (Input.GetAxis("Mouse X") < 0) wont work (at least it's not supposed to).

You should get the center of the screen by doing Screen.width/2.

Then, you can do x<... x>....

So something like:

     int middleScreen = Screen.width/2;
     if(Input.GetAxis("Mouse X") < middleScreen) {
         // do something
     }
     if(Input.GetAxis("Mouse X") > middleScreen) {
         // do something
     }

I hope this is what you meant :)

Comment
Add comment · Show 6 · 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
avatar image LeftyTwoGuns · Aug 07, 2013 at 01:14 AM 0
Share

Thanks for explaining that, I didn't know the "0" represented the bottom corner of the screen.

After some more reading I decided that having the turrets rotate to aim at the mouse cursor would be a better solution. But using this simple mouse look script:

     public float speed;
  
     void FixedUpdate () 
     {
         
         Plane playerPlane = new Plane(Vector3.forward, transform.position);
  
         
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  
         
         float hitdist = 0.0f;
         
         if (playerPlane.Raycast (ray, out hitdist)) 
         {
             
             Vector3 targetPoint = ray.GetPoint(hitdist);
  
             
             Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
  
             
             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.time);
         }
         
 }

I get another issue where the turret is rolling on its side, rather than rotating back and forth like I intend. Why is it doing this? To clarify, the view is 2D and the turret is at the bottom, ai$$anonymous$$g upwards like Centipede.

avatar image dorpeleg · Aug 07, 2013 at 06:39 AM 0
Share

I'm guessing you are rotating the turret on the wrong axis/more then one axis.

$$anonymous$$eep the turret selected in the hierarchy and look at the inspector while the game is running.

See if it rotates on more then one axis.

avatar image LeftyTwoGuns · Aug 07, 2013 at 11:50 PM 0
Share

It's actually rotating on all the axes, it seems. I added a simple Rotation Constraint line at the beginning of the script:

 rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;

But that just keeps the object from rotating at all.

avatar image dorpeleg · Aug 08, 2013 at 06:55 AM 0
Share

Don't constrain it.

You might want to try Transform.LookAt ins$$anonymous$$d of Quaternion.LookRotation.

Working with vector3 is easier then working with quaternion.

Try something like:

 Vector3 originalRotation = transform.rotation.eulerAngles;
 
 transform.LookAt(target);
 
 // now rotate back the axis that you don't want to move (only rotating the z in this example
 transform.rotation.eulerAngles = new Vector3(originalRotation.x,originalRotation.y, transform.rotation.z);

Let me know if it works.

avatar image LeftyTwoGuns · Aug 09, 2013 at 01:55 AM 0
Share

This is how I modified it:

 public float speed;
     
     
  
     void FixedUpdate () 
     {
         
         Plane playerPlane = new Plane(Vector3.forward, transform.position);
  
         
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  
 
         float hitdist = 0.0f;
 
         if (playerPlane.Raycast (ray, out hitdist)) 
         {
     
             Vector3 targetPoint = ray.GetPoint(hitdist);
             
             Vector3 originalRotation = transform.rotation.eulerAngles;
             
             transform.LookAt(targetPoint);
  
             transform.rotation.eulerAngles = new Vector3(originalRotation.x, originalRotation.y, transform.rotation.z);
             
         }
         
     }

But I get the error "Cannot modify a value type return value of `UnityEngine.Transform.rotation'. Consider storing the value in a temporary variable" on line (46,27). Doing some research, that error usually occurs when you try to specify just one axis ins$$anonymous$$d of all of them at once. Hasn't that already been done in this script? Unless I'm using your example wrong.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Look around when holding down mousebutton 1 Answer

make turret point where mouse points 3 Answers

rotate object to face mouse cursor x/z top down shooter 0 Answers

How do I get the player to correctly rotate toward the mouse? 1 Answer

Airplane Control, Aim ... Lerp and Vectors 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