Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Tarnthelos · Jul 16, 2015 at 07:04 AM · c#rotate objecteulerangles

Issue rotating objects with input from the mouse wheel

Hey there everyone, first time poster here.

So, I've got a c# script that is supposed to get input from the mouse wheel and then use that input to rotate a game object.

I can get input from the mouse wheel no problem, but as soon as I scroll the wheel the game object locks into an upright position and will jiggle around a little bit if I move the mouse wheel, but won't rotate like it should. Been banging my head all day trying to figure this out, so any assistance with this would be greatly appreciated. The script is pretty short, so I'll post the thing in its entirety here.

 using UnityEngine;
 using System.Collections;
 
 public class Adjust_Sights : MonoBehaviour {
 
     private float rot;        //stores the rotation of the mouse wheel
     private float speed;      //multiplier for the mouse wheel input
     private Quaternion storePos; //stores the rotation of the attached gameObject
 
 
     // Use this for initialization
     void Start () {
 
         speed = 10.0f;
 
     }
     
     // Update is called once per frame
     void Update () {
 
         rot = Input.GetAxis("Mouse ScrollWheel") * speed * Time.deltaTime;  //gets the mouse wheel input and stores it in ro
         storePos = gameObject.transform.rotation;  //keeps storePos up to date
         
         
 
         if (rot != 0)
         {
 
             transform.eulerAngles = new Vector3(storePos.x, storePos.y, rot); //Doest the actual rotation of the object
         }
     }
 }
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
1
Best Answer

Answer by maccabbe · Jul 16, 2015 at 07:12 AM

First, gameObject.transform.rotation returns a Quaternion (4 parts w, x, y, z). You probably want transform.eulerAngles which is a Vector3 (3 parts x, y, z) for storePos.

Second, you probably don't want the z rotation to be directly proportional to how much the mousewheel has moved but rather added onto the current z rotation.

Try

  using UnityEngine;
  using System.Collections;
  
  public class Adjust_Sights : MonoBehaviour {
  
      private float rot;        //stores the rotation of the mouse wheel
      private float speed;      //multiplier for the mouse wheel input
      private Vector3 storePos; //stores the rotation of the attached gameObject
  
  
      // Use this for initialization
      void Start () { 
          speed = 10.0f;
          storePos = gameObject.transform.eulerAngles;  //keeps storePos up to date 
      }
      
      // Update is called once per frame
      void Update () {
  
          rot = Input.GetAxis("Mouse ScrollWheel") * speed * Time.deltaTime;          
          
  
          if (rot != 0)
          {
              storePos= new Vector3(storePos.x, storePos.y, storePos.z+rot);
              transform.eulerAngles = storePos;
          }
      }
  }

Comment
Add comment · Show 1 · 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 Tarnthelos · Jul 16, 2015 at 03:02 PM 0
Share

This worked perfectly, thank you! I just had to adjust speed and put the storePos = gameObject... back into update, since the script is attached to an object that will be walking around and it will need to be kept in the same x and y position in relation to its parented object. Again, thank you so much!

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Odd discrepancy with EulerAngles (Circular motion, C#) 1 Answer

Rotation of player not working 0 Answers

How do I set an objects euler rotations to match the objects transform in C#? 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