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 kjones3d · Feb 29, 2016 at 09:17 PM · movement script

Object is moving the wrong number of units.

I'm trying to make a game based on the Space Shooter tutorial, but with movement similar to Crossy Road.

What is supposed to happen is when the player presses the Left or Right buttons, the ship will slide 10 units in that direction. I thought I had it pegged down, but I added in code to make the ship rotate as it slid over and I was trying to clean up some messy code and now some funny business is happening. The first time I press a direction after starting the game the ship moves way farther than 10 units in that direction. After that calms down, the ship will move 20 units and on occasion it will go the 10 it is supposed to go.

Am I making a mistake somewhere that I can't see? Is there a better way I can achieve this movement? Here is my code and thanks in advance.

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour 
 {
     public float speed = 50;
     public float tiltAmt = 30;
     public float currentTilt = 0;
 
     private Rigidbody rb;
     public Vector3 moveDest = new Vector3(0,0,0);
     private float sqrRemainingDistance;
 
     void Start () 
     {
         rb = GetComponent<Rigidbody> ();
     }
     
     void FixedUpdate()
     {
         sqrRemainingDistance = (rb.transform.position - moveDest).sqrMagnitude;
     
         if (Input.GetKeyDown (KeyCode.LeftArrow)) {
             moveDest.x += -10;
             rb.rotation = Quaternion.Euler(0f,0f, tiltAmt);
         }
         if (Input.GetKeyDown (KeyCode.RightArrow)) {
             moveDest.x += 10;
             rb.rotation = Quaternion.Euler(0f,0f, -tiltAmt);
         }
 
         if (sqrRemainingDistance > float.Epsilon) 
         {
             Vector3 newPosition = Vector3.MoveTowards(rb.transform.position, moveDest, Time.deltaTime * speed);
 
             rb.MovePosition (newPosition);
 
             sqrRemainingDistance = (rb.transform.position - moveDest).sqrMagnitude;
         }
 
         if(rb.rotation.z != 0)
             rb.rotation = Quaternion.RotateTowards(rb.rotation, Quaternion.Euler (0f,0f,0f), Time.deltaTime * (speed * 2));
     }
 
 }
 

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 tessellation · Mar 01, 2016 at 08:16 AM

FixedUpdate can be called multiple times in one frame, depending on the actual frame rate of your scene. According to the documentation of Input.GetKeyDown, it should be called within the Update() event, since it is reset at the end of the frame and Update() is called only once per frame.

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 kjones3d · Mar 01, 2016 at 02:49 PM 1
Share

Thank you very much. I was just under the impression that if you were moving objects using physics, you were only supposed to use FixedUpdate. I also didn't realize FixedUpdate could be called more than once a frame, but that makes sense based on the result I was getting.

I put my if statements for the button presses into Update() and it's working well now.

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

49 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

Related Questions

Isometric movement for MoveTowards 0 Answers

Input System: How to declare interaction types to compare to callback context? 0 Answers

(2D) My Character wont stop moving/sliding when I let go of the button 1 Answer

How do I make a centipede/conga line out of game objects? 0 Answers

2D Board Game Movement Script Results in Empty Calculations 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