Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 louisebishop1 · Jan 31, 2013 at 12:58 AM · rotationmouselimitdegrees

Limit rotation of weapons with mouse help

I have a robot who shoots paintballs out of two paintball guns mounted as arms. These rotate to follow the mouse and shoot in that direction when fired. At the moment the guns face backwards sometimes when the mouse goes behind the player. I need them to constantly face forward- do I need to limit rotation? Here is my code so far..

 using UnityEngine;
 using System.Collections;
  
 public class MyScript : MonoBehaviour {
  
     private Vector3 inputRotation;
     private Vector3 mousePlacement;
     private Vector3 screenCentre;
  
     void Update () {
     FindStuff();
     transform.rotation = Quaternion.LookRotation(inputRotation);
     }
 
     void FindStuff () {
     screenCentre = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
     mousePlacement = Input.mousePosition;
     mousePlacement.z = mousePlacement.y;
     mousePlacement.y = 0;
     inputRotation = mousePlacement - screenCentre;
     }
 }

Any help or pointers would be great!

Thanks

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 robertbu · Jan 31, 2013 at 01:42 AM

Quick, untested idea. Limit your mousePlacement.z to positive values >= 1. Just something like:

 if (mousePlacement.z < 1) mousePlacement.z = 1;

This should accomplish two goals. First the point you look at will always be in front of the robot, so his guns will never face backward. Second you will never produce an inputRotation of (0,0,0). According to the docs, LookRotation() called with (0,0,0) will log an error.

Comment
Add comment · Show 2 · 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 louisebishop1 · Jan 31, 2013 at 03:29 AM 0
Share

It doesn't seem to make any difference- perhaps I am placing it incorrectly. I usually use JavaScript.

Here is my code

 using UnityEngine;
 using System.Collections;
  
 public class $$anonymous$$yScript : $$anonymous$$onoBehaviour {
  
     private Vector3 inputRotation;
     private Vector3 mousePlacement;
     private Vector3 screenCentre;
  
     void Update () {
     FindCrap();
     transform.rotation = Quaternion.LookRotation(inputRotation);
     }
 
     void FindCrap () {
     screenCentre = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
     mousePlacement = Input.mousePosition;
     mousePlacement.z = mousePlacement.y;
     mousePlacement.y = 0;
     if (mousePlacement.z < 1) mousePlacement.z = 1;    
         
     inputRotation = mousePlacement - screenCentre;
     }
 }
  
avatar image robertbu · Jan 31, 2013 at 05:33 AM 0
Share

I played a bit. Here is a mod to your code that I think gives you what you want.

 public class $$anonymous$$yScript : $$anonymous$$onoBehaviour {
 
     private Vector3 inputRotation;
     private Vector3 mousePlacement;
     private Vector3 screenCentre;
 
     void Update () {
     FindStuff();
     transform.rotation = Quaternion.LookRotation(inputRotation);
     }
 
     void FindStuff () {
     screenCentre = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
     mousePlacement = Input.mousePosition - screenCentre;
     //mousePlacement.z =  mousePlacement.y;
     mousePlacement.z =  100; // <--- Set to get the sensitivity
     mousePlacement.y = 0;
     inputRotation = mousePlacement; 
     }
 }

Note that placing mouse 'Y' in 'Z' is going to give inconsistent behavior in that the amount of rotation will change depending on the 'Y' location of the mouse. Pick a value that gives the mouse sensitivity you want and just assign it to z.

Using screen units to do calculations can cause problems as resolutions change. Here is a different way of doing your rotation:

 public class $$anonymous$$yScript : $$anonymous$$onoBehaviour {
 
     void Update () {
         Vector3 v3T = Input.mousePosition;
         v3T.z = 20; 
         v3T = Camera.main.ScreenToWorldPoint (v3T);
         v3T.y = transform.position.y;  
         transform.rotation = Quaternion.LookRotation(v3T);
     }
 }

Note that the Z value used in ScreenToWorldPoint() is the distance in front of the camera. For this code to work, the value has to be greater than the distance between camera and the object rotating.

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

9 People are following this question.

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

Related Questions

Limit Object Angle 2 Answers

Clamp rotation of a GameObject 1 Answer

how can i get Y rotation of Transform in degree ?? 1 Answer

Rotation of Object on single axis in direction of the mouse position 0 Answers

Slerp/Rotational Problem Query 1 Answer


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