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
1
Question by brumlik · Jun 19, 2014 at 11:01 AM · rotationmovement script

Need help with the movement+rotation C# script

I'm just starting to learn Unity and I've written a basic script for discrete movement + rotation in C#, using a movement script I found online as a basis. It uses WASD to move the rigid body around (without rotating it) and Q and E to rotate it 90 degrees left and right. It seems to work for the most part - however, the z coordinate behaves a bit weird when I follow a route that combines movement and rotation.

Namely, if I start moving from point A(x,y,z) and move forward->rotate right->move forward->rotate right->move forward->rotate right->move forward, coming back to point A again, the new z' coordinate is slightly different from the starting one. As a result, I arrive to point A'(x,y,z') instead of the original A(x,y,z). Same goes for rotating left.

That must have something to do with rotation, because if I only use the WASD movement (without rotation), my rigidbody returns to the same starting coordinates A(x,y,z) without any problem.

To illustrate, here are some screenshots. Starting position. The coordinates are (0, 10, 0). alt text

Movement (small gif):

alt text

Resulting position. The coordinates are (0, 10, -1.490116e-08).

alt text

Where does the "-1.490116e-08" come from and how do I get rid of it? Here is my script:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (Rigidbody))]
 [RequireComponent (typeof (CapsuleCollider))]
 
 public class Movement_new : MonoBehaviour {
     
     public float speed = 1.0f;
     private Vector3 endpos;
     private bool moving = false;
     private float rotationVal = 0.0f;
     private Quaternion qTo;
 
     void Start () {
         endpos = transform.position;
         qTo = transform.rotation;
     }
     
     void Awake () {
         rigidbody.freezeRotation = true;
         rigidbody.useGravity = false;
     }
     
     void Update () {
         
         if (moving && (transform.position == endpos) && (transform.rotation == qTo))
             moving = false;
         
         if(!moving && Input.GetKeyDown (KeyCode.W)){
             moving = true;
             endpos = transform.position + transform.forward;
         }
         
         if(!moving && Input.GetKeyDown (KeyCode.S)){
             moving = true;
             endpos = transform.position - transform.forward;
         }
         
         if(!moving && Input.GetKeyDown (KeyCode.A)){
             moving = true;
             endpos = transform.position - transform.right;
         }
         
         if(!moving && Input.GetKeyDown (KeyCode.D)){
             moving = true;
             endpos = transform.position + transform.right;
         }
         
         if (!moving && Input.GetKeyDown (KeyCode.E)) {
                         moving = true;
                         rotationVal += 90.0f;
                         qTo = Quaternion.Euler (0.0f, rotationVal, 0.0f);
                 }
         if (!moving && Input.GetKeyDown (KeyCode.Q)) {
                         moving = true;
                         rotationVal -= 90.0f;
                         qTo = Quaternion.Euler (0.0f, rotationVal, 0.0f);
                 }
 
         transform.rotation = Quaternion.RotateTowards (transform.rotation, qTo, 100 * Time.deltaTime);
         transform.position = Vector3.MoveTowards(transform.position, endpos, Time.deltaTime * speed);
 
         }
 
 }

I'm new to Unity and programming alike, so I must be doing something wrong. Any comments/advice would be much appreciated.

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 Ricewind1 · Jun 19, 2014 at 11:07 AM 1
Share

It's such a small number, a few things come to $$anonymous$$d first. It might be the friction of the rigidbody (try turning that off). Also, to be sure, you could try the following:

 qTo = Quaternion.Euler (qTo.rotation.x, qTorotation.y+rotationVal, qTo.rotation.z);
avatar image Nerevar · Jun 19, 2014 at 11:10 AM 1
Share

Well it is just a precision error of the computer, unity is working in floats that is basically 7 digits precision so "-1.490116e-08" is actually Zero, I don't think you have to worry about that nor that it may cause you position issues.

1 Reply

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

Answer by tanoshimi · Jun 19, 2014 at 11:08 AM

Hi,

First of all, welcome to UA, and thanks for taking the time to write a detailed description (and animated gifs!) of your problem.

"-1.490116e-08" is scientific notation for the number -0.00000001490116 which is a number very, very close to zero (the coordinate value you were expecting) but not quite.

If you're new to programming then this might come as a bit of a surprise, but a lot of calculations performed by computers are not as precise as you think they might be, because they're based on floating-point arithmetic (that's what the float keyword and the F you see after some numbers refers to).

When you work with integers, 0 + 10 - 10 = 0 exactly, on the nail. When you work with floats, it might not be.

There's nothing you're doing wrong, and there's nothing you need to do to "fix" it, other than the fact you should never expect nor try to work with floating point numbers as if they contain exact values, because they don't - there's an inherent degree of imprecision in floating-point maths.

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 brumlik · Jun 19, 2014 at 11:29 AM 0
Share

Thanks! I wasn't aware that float number calculations work like that. I'll be sure to keep that in $$anonymous$$d.

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

Player rotating towards direction of movement in 3D 1 Answer

Flip over an object (smooth transition) 3 Answers

Determining the rotation of an object based on its upcoming position? 1 Answer

Rotating character to a direction (2.5d) 1 Answer

Unity 2D movement and rotate sprite to the direction it is moving in 1 Answer


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