• 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 RC020 · Mar 18, 2016 at 01:15 AM · c#rotationbeginner

What is wrong with my code? C# beginner

Hi there, I am trying to create a game in where an object in unity rotates 90 degrees when the mouse is clicked. I have made it so that the object moves to the right as can be seen in the code below. So I tried to write a similar bit of code for this rotation script but it's not working for me. Can anybody please tell me where I have gone wrong? Thanks!

using UnityEngine; using System.Collections;

public class Movement : MonoBehaviour {

 // Use this for initialization
 void Start()
 {}

 // Update is called once per frame
 void Update () {

     Rigidbody2D rb = GetComponent<Rigidbody2D>();
     // Change the mass of the object's Rigidbody.
     rb.mass = 10f;
     rb.AddForce(Vector2.right * 10f);

 }

}

public class Rotate : MonoBehaviour { void OnMouseDown()
{

     Rigidbody2D rb = GetComponent<Rigidbody2D>();
     // Rotate the object's Rigidbody.
     rb.transform.Rotate(new Vector3( 0, 90, 0));

 }
 }
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 alexisrabadan · Mar 18, 2016 at 01:23 AM

Try

rb.transform.rotation = Quaternion.Euler(0f, 90f, 0f);

and while you are at it, it is good practice to cache the rigidbody by declaring the class variable Rigidbody rb; above the Start method and placing

rb = GetComponent<Rigidbody2D>();

into the Start method

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 Dudicus · Mar 18, 2016 at 01:32 AM 0
Share

This should work!

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

129 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 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

Wrong .rotation.eulerAngles.x output 1 Answer

Rotating a Model 1 Answer

How to access y rotation of an object as a variable or value 1 Answer

How do you change your z axis in a movement script. 1 Answer

Recognize when ever camera looks up and turns back down 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges