• 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 /
  • Help Room /
avatar image
0
Question by mfarrell806 · Dec 09, 2016 at 08:28 PM · rotationonmousedown

why does object rotate only ONCE?

The below code is attached to a 3d cube. It does almost what I want it to, that being rotate the object 90 degrees on mouse click.

However the problem is possibly two issues.

1) it only rotates once when the mouse is clicked, and then additional clicks do nothing. so basically it only rotates one time, and then it ignores the mouse click.

 using UnityEngine;
 using System.Collections;
 
 
 public class Rot : MonoBehaviour
 {
     private Vector3 currentRotation;
 
     // Use this for initialization
     void OnMouseDown()
     {
         currentRotation = gameObject.transform.eulerAngles;
         transform.rotation = Quaternion.Euler(0, 0, 90);
     }
 }

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 PizzaPie · Dec 09, 2016 at 08:40 PM

Your problem is that you set the Rotation on a specific point with the`transform.rotation = Quaternion.Euler(0, 0, 90);` it works the first time because you have different rotation from the (0,0,90) afterwards it sets it every time on that specific rotation. You need to take the initial rotation as you do with the currentRotation , add the rotation you want to it and then set it to the gameObject.

 transform.rotation = Quaternion.Euler(0, 0, currentRotation.z + 90f);

try different numbers to achieve the rotation you want.

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 mfarrell806 · Dec 09, 2016 at 08:44 PM 0
Share

And how do I do that?

Sorry if that is a dense answer on my part, but I'm only like 5 days into Unity scripting!

avatar image mfarrell806 · Dec 09, 2016 at 10:29 PM 0
Share

This works ..EXACTLY as desired....now on to the next issue....

Thanks so much!

avatar image
0

Answer by RobAnthem · Dec 09, 2016 at 08:53 PM

Instead of setting the quaternion rotation the same each time, try making it additive.

          currentRotation = gameObject.transform.eulerAngles;
          transform.rotation * Quaternion.Euler(0, 0, 90);

Except the 90 will obviously rotate it too much, so you'll need to do a little math work, but essentially you need to add to the rotation, not replace it.

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 mfarrell806 · Dec 09, 2016 at 10:04 PM 0
Share

first thanks again,

however when I change my code to what you posted, I now just get a bunch of errors in the editor.

Severity Code Description Project File Line Suppression State Warning CS0168 The variable 'Quaternion' is declared but never used New Unity Project C:\Users\Public\Documents\Unity Projects\New Unity Project\Assets\Rot.cs 13 Active Error CS1003 Syntax error, ',' expected New Unity Project C:\Users\Public\Documents\Unity Projects\New Unity Project\Assets\Rot.cs 13 Active Error CS1002 ; expected New Unity Project C:\Users\Public\Documents\Unity Projects\New Unity Project\Assets\Rot.cs 13 Active Error CS0246 The type or namespace name 'transform' could not be found (are you missing a using directive or an assembly reference?) New Unity Project C:\Users\Public\Documents\Unity Projects\New Unity Project\Assets\Rot.cs 13 Active Error CS0214 Pointers and fixed size buffers may only be used in an unsafe context New Unity Project C:\Users\Public\Documents\Unity Projects\New Unity Project\Assets\Rot.cs 13 Active Error CS0208 Cannot take the address of, get the size of, or declare a pointer to a managed type ('transform.rotation') New Unity Project C:\Users\Public\Documents\Unity Projects\New Unity Project\Assets\Rot.cs 13 Active Error CS0103 The name 'Euler' does not exist in the current context New Unity Project C:\Users\Public\Documents\Unity Projects\New Unity Project\Assets\Rot.cs 13 Active

Also I want/need 90 degree rotations, not almost 90.

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

96 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

Related Questions

OnMouseDrag() stopped working after few clicks 0 Answers

Rotation of game objects 0 Answers

Rotate Player w/Touch Input Controls 0 Answers

Problem with movement while rotating the character 0 Answers

How to make an object do a multiple tasks forever 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