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 aled96 · Mar 22, 2014 at 01:44 PM · rotationspeedangle

Rotation strange

Hi, I have a character that can turn left or right, based on the the button that you press. But there is a problem, sometimes the character stops to turn and sometimes goes faster, sometimes goes slower.... I don't know why this happened... This is my code:

 float ang,y;
 Vector3 moveDirection,angle2,move;


void Update () {

     moveDirection = this.transform.position;
     angle = this.transform.rotation;
     angle.y = angle.y % (2 * Mathf.PI);
     anim.SetBool ("IsWalking", false);
     
     if (Input.GetKey (KeyCode.RightArrow)) {
         this.transform.Rotate (0, angle.y + 1f, 0);
     } else if (Input.GetKey (KeyCode.LeftArrow)) {    
         this.transform.Rotate (0, angle.y - 1f, 0);
     }
     if (Input.GetKey (KeyCode.UpArrow)) {    
         float a = 0.04f;
         angle2 = transform.eulerAngles;
         ang = (Mathf.Abs (Mathf.Deg2Rad * angle2.y)) % (2 * Mathf.PI);
         moveDirection.x += a * Mathf.Sin (ang);
         moveDirection.z += a * Mathf.Cos (ang);
     }
     this.transform.position = moveDirection;
 }

Help me please :D :D

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
Best Answer

Answer by robertbu · Mar 22, 2014 at 02:49 PM

Your rotate has a couple of problems. Transform.Rotate() incrementally changes the rotation. It is a relative rotation. You are trying to use it as an absolute rotation. In addition you are not scaling by Time.deltaTime. Try this this instead:

 transform.Rotate(0,rotationSpeed * Time.deltaTime, 0);

'rotationSpeed' would be declared at the top of the file and be degrees per second.

You are also not scaling your movements by Time.deltaTime. The amount of time that passes each frame varies. When there is a heavier load, then the fps drops and deltaTime increases. And your movement code is going the long way around. Try this:

 if (Input.GetKey (KeyCode.UpArrow)) {  
   transform.position = transform.forward * speed * Time.deltaTime;
 }

Alternate that does the same thing:

 if (Input.GetKey (KeyCode.UpArrow)) {  
     transform.Translate(0,0,speed * Time.deltaTime);
 }

'speed' will be a variable you define at the top of the file and will be set to the units per second you want to move.

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 aled96 · Mar 24, 2014 at 04:04 PM 0
Share

I tried but it doesn't work, can you change my code please ? :)

avatar image robertbu · Mar 24, 2014 at 04:24 PM 0
Share

Here is the essence of my suggestion as a complete script. Start with a new scene, add a cube, add this script. Use the arrow keys to rotate and move. Once you've verified that the logic works, you can integrate the changes back into your code.

 using UnityEngine;
 using System.Collections;
 
 public class Bug25b : $$anonymous$$onoBehaviour {
     float rotationSpeed = 135.0f;  // Degrees per second;
     float speed = 3.0f; // Units per second
 
     void Update () {
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow)) {
             transform.Rotate(0,rotationSpeed * Time.deltaTime, 0);
         } else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow)) { 
             transform.Rotate(0,-rotationSpeed * Time.deltaTime, 0);
         }
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow)) {  
             transform.Translate(0,0,speed * Time.deltaTime);
         }
     }
  }  
avatar image aled96 · Mar 24, 2014 at 04:40 PM 0
Share

Thank you so much :)

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

20 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

Related Questions

Speed of player depends on angle of rotation 1 Answer

Problems caused by non-unique Euler angle solutions 1 Answer

Restricting rotation to multiples of 15? 1 Answer

How do I apply rotations to parent child using Rigidbody? 0 Answers

90 Degree stopping rotation on y-axis issue 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