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));
}
}
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.
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
![](https://koobas.hobune.stream/wayback/20220612070106im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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