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 ForgeHand · Nov 26, 2012 at 10:45 PM · 2drotationsmooth

Rotating a character smoothly around 90 degrees

Hi I'm working on my first game, a top down 2D game. I need the player to rotate smoothly 90 degrees around the local y axis over half a second. At the moment I am using

 GameObject.Find("Player").transform.Rotate(Vector3(0, 90, 0));

which rotates the character fine, but instantly rather than smoothly over around half a second as I need it to. I have tried using Time.deltaTime but when used the character rotates a small amount rather than the full 90 degrees.

Here's all I have so far

 private var canMove : boolean;
 
 function Start () {
 canMove = true;
 }
 
 function Update () {
  if(canMove){
   if(Input.GetKeyDown("left")){
    TurnLeft();
   }
   if(Input.GetKeyDown("right")){
    TurnRight();
   }
  }
 }
 
 function TurnLeft () {
  GameObject.Find("Player").transform.Rotate(Vector3(0, 90, 0));
 }
 
 function TurnRight () {
  GameObject.Find("Player").transform.Rotate(Vector3(0, -90, 0));
 }

This might be completely wrong but I am extremely new to coding still. Hope you can help.

Comment
Add comment · Show 2
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 Sonaten · Nov 28, 2012 at 11:41 AM 0
Share

First of all. Never use .Find("nameOfObject") in something called through the Update-function. As .Find searches through all objects in the scene. You could instantiate it as a variable in function Start().

This is however not related to your question.

avatar image ForgeHand · Nov 30, 2012 at 09:07 AM 0
Share

Okay, it's still useful though, thank you for the advice!

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by DeveshPandey · Nov 28, 2012 at 07:04 AM

Try this :

private var canMove : boolean;
private var turnLeft : boolean;
private var turnRight : boolean;
private var rotationVal : float = 0f;
private var temp : float = 0f;
private var myTransform;

 function Start () {
 canMove = true;
 myTransform = GameObject.Find("Player").transform;
 }
 
 function Update () {
  if(canMove){
   if(Input.GetKeyDown("left")){
      turnLeft = true;
    //TurnLeft();
   }
   if(Input.GetKeyDown("right")){
     turnRight = true;
    //TurnRight();
   }
  }
 }
 
 function LateUpdate()
 {
     if(turnLeft)
     {
          if(temp > 90){
                turnLeft = false;
             temp = 0f;
          }
          else{
                rotationVal = Mathf.Lerp(0f,90f,Time.deltaTime);
                myTransform .Rotate(Vector3(0, rotationVal, 0));
                temp += rotationVal;
           }
         
     }
     else if(turnRight)
     {
         if(temp < -90){
                turnRight = false;
             temp = 0f;
          }
          else{
                rotationVal = Mathf.Lerp(0f,-90f,Time.deltaTime);
                myTransform .Rotate(Vector3(0, rotationVal, 0));
                temp += rotationVal;
           }
         
     }
 }
 
Comment
Add comment · Show 3 · 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 DeveshPandey · Nov 28, 2012 at 11:27 AM 0
Share

vote up and accept this answer if its working :)

avatar image ForgeHand · Nov 30, 2012 at 09:09 AM 0
Share

That worked perfectly thank you!

avatar image DeveshPandey · Nov 30, 2012 at 09:16 AM 0
Share

please make a vote up, to show the perfect one! thanks!

avatar image
0

Answer by Maulik2208 · Nov 27, 2012 at 10:30 AM

Hope This will work for you.....it's works for me....

transform.Rotate(new Vector3(0, rotSpeed * Time.deltaTime, 0));

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 Maulik2208 · Nov 27, 2012 at 10:32 AM 0
Share

here define rotspeed to as per your requirement just like int rotSpeed = x;

avatar image Maulik2208 · Nov 28, 2012 at 06:18 AM 0
Share

IF you found this useful then let others know that click the Thumb-up symbol and tick mark as correct answer....it will help others to find out the things properly....Thanks...

avatar image ForgeHand · Nov 30, 2012 at 09:12 AM 0
Share

I still found this soultion had the same problem, it started incresing towards 90 degrees but stopped very quickly

avatar image ForgeHand · Nov 30, 2012 at 09:13 AM 0
Share

Thank you for taking the time to reply though!

avatar image Maulik2208 · Nov 30, 2012 at 10:00 AM 0
Share

No worries I am happy to Help

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

13 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

Related Questions

Smooth 2D rotation unexpected 1 Answer

How to smoothly rotate object in direction of analog joystick? 1 Answer

How to rotate an RigidBody2D object to an angle and get back to the previous position in the smooth way? 1 Answer

Mathf.LerpAngle not working after RigidBody2D collisions 1 Answer

How to use one animation and Animator Controller on multiple objects? [2D] 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