Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Solhart · Nov 29, 2016 at 03:48 PM · transformrotateaxisrotate objectrotation axis

how to rotate a mesh with GetMouseButton with specific rotation value

Hello there,

I'm really new to C# scripting (really new here :3) I'm trying to rotate a mesh with GetMouseButton and has a specific rotation value when i click on it.

My idea is i wan to click the mesh, and make it rotate at a specific value of -90 on the y-axis, and when i click again, i want it to return back the y-axis to 0.

here is my script:

using UnityEngine; using System.Collections;

public class playerscript : MonoBehaviour {

 public float speed;
 private Vector3 dir;


 // Use this for initialization
 void Start () {
         dir = Vector3.zero;

 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetMouseButton (0)) {
         
         if (dir == Vector3.forward) {
             dir = Vector3.left;
             transform.Rotate (0, -90, 0);
         } else {
             dir = Vector3.forward;
             transform.Rotate (0, 0, 0);
         }

     }

     float amoutToMove = speed * Time.deltaTime;
     transform.Translate (dir * amoutToMove);
 }

}

The problem that i'm having is when i hold the mesh, it will rotate in full circle.. and i would like to prevent that haha.

The mesh will also move forward when i click the mesh.

Could anyone help me with this? I much appreciate your help :) Thank you

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

Answer by SmomoGame · Nov 29, 2016 at 04:14 PM

transform.Rotate is "Relative", means "Turn around some degree from CURRENT degree" not "Look at specific degree"

In your code, the mesh will rotate -90 first, then rotate 0 degree (nothing happen), and continue to rotate -90.

You may want to use transform.eulerAngles to set a specific angle constantly

Modify "transform.Rotate" to

 transform.eulerAngles = new Vector3(0,-90f,0) ;

-90 or 0

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 Solhart · Nov 29, 2016 at 04:30 PM

It worked! thank you SmomoGame hahaha I'm really glad you replied haha

but i have a new problem now.. When i click, the mesh did turn at -90, but now the mesh is moving backwards instead of going left.

I'm sry, i'm rly a noob with this haha

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 SmomoGame · Nov 29, 2016 at 04:38 PM 0
Share

transform.eulerAngles is "World" coordinate.

If you turn back with -90 degree, means your initial direction is not world's forward, So just add some offset to the value.

maybe 0 = your left , 90 = your forward (try yourself)

avatar image Solhart SmomoGame · Nov 29, 2016 at 05:04 PM 0
Share

aaa ic.. no wonder hahaha orait2 ill give it a try, thx alot mate haha

*brofist from $$anonymous$$alaysia

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

84 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

Related Questions

Rotating on other axis 1 Answer

How to object rotate on a single axis towards another object? C# 2 Answers

Rotate an Object while moving forward using empty gameObject 0 Answers

How to rotate an object according to the camera's view? 1 Answer

how do I rotate an object without affecting later rotation? 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