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 rdmartyr7 · Nov 09, 2014 at 02:47 PM · rotate objectz axismouse click

How to make an object roate 180 on Z axis on each mouse click

I'm working on a project for school and I need an object to rotate 180 degrees when selected with the mouse. The first attempt only got the object to rotate a little each time however with some assistance I came up with the code below. The issue however is that while it rotates the first time 180, every other time it rotates 360. How do I correct my script so that it is operating properly (rotating 180 each time it is selected - basically flipping to upside down) Below is the code I currently have that is giving me the issue.

Many thanks,

Chris

using UnityEngine; using System.Collections;

public class Select3 : MonoBehaviour { float speed = 0;

 void Start () 
 {
 
 }
 
 void Update () 
 {
     transform.Rotate(0, 0, Time.deltaTime*speed);      

     Debug.Log (gameObject.transform.rotation.z);       

     Debug.Log (gameObject.transform.rotation.z *180);  


     if (Mathf.Abs(gameObject.transform.rotation.z)*180 >= 179.995)  
     {
         speed = 0; 
     }
 }

 void OnMouseDown()
 {
     speed = 100; 
 
 }

}

Comment
Add comment · Show 1
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 SmalleeStudio · Nov 10, 2014 at 06:57 AM 0
Share

transform.rotation.z return a "Rotator" not a float, please use transform.rotation.eulerAngles.z

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SmalleeStudio · Nov 10, 2014 at 06:52 AM

 void Start () {
 
 }
 
 void Update () {
     
     alpha += speed ;
     alpha = Mathf.Clamp(alpha, 0, 1) ;
     
     transform.rotation.eulerAngles.z = Mathf.Lerp(0, 180, alpha) ;
 }
 
 void OnMouseDown(){
     speed*=-1 ;
 }


Comment
Add comment · Show 5 · 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 SmalleeStudio · Nov 10, 2014 at 06:56 AM 0
Share

"speed" variable must < 1 or you will see the instantly change, suggest 0.05

avatar image rdmartyr7 · Nov 10, 2014 at 12:40 PM 0
Share

I'm sorry I tried it and I get a lot of errors. I tried as new code and also put it in to my existing code. The big errors that I get are that

"The name 'alpha does not exist in the current context"

Cannot modify a value type return value of "UnityEngine.Quaternion.eulerAngles". Consider storing the value in a temporary variable

Cannot modify a value type return value of "UnityEngine.Transform.rotation'. Consider storing the value in a temporary variable

Those are the errors I get if it's a new script and if it's compiled with the original. I appreciate the help in suggesting a code, just not sure why I get the errors.

$$anonymous$$any Thanks

Chris

avatar image Scribe · Nov 10, 2014 at 12:48 PM 0
Share

You need to define the variables alpha and speed as well as another variable of type Vector3 which you should save transform.rotation.eulerAngles to, then modify, then set transform.rotation.eulerAngles to. Perhaps do a few tutorials first if you don't understand variables :)

avatar image rdmartyr7 · Nov 10, 2014 at 01:44 PM 0
Share

thank you Scribe. I have been doing tutorials. I'm just not picking it up that easily. However I just didn't understand what alpha meant. I'll go back over the tutorials.

avatar image SmalleeStudio · Nov 10, 2014 at 06:17 PM 0
Share

Sorry about unclear code. And Thank for Scribe's comment.
alpha means the value's position "property" between two float.
If alpha = 0, the value will return the first value in $$anonymous$$athf.Lerp(first,second,alpha) . If alpha=1, return second value.
So if alpha =0.5, it may be 90, the average.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Limit rotation of z axis of a 2d object which rotates with mouse click and drag 1 Answer

Unity2D problem with negative z rotation shooting bullet towards mouse click 1 Answer

Rotating Produces Odd Results [Unity 2D] 1 Answer

Rotation around Z axis wont lock correctly (2D) 1 Answer

How to rotate a Character Controller on the X or Z axis ? (Not for movement) 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