Movement sometimes not working
Hello!. I want to make the player object move on click and then stop at a predetermined target position, if its left move right and if it's right move to the left, but the code I made sometimes gets stuck, the object seems to start moving but advances only a little bit and the bool shouldMove appears as false, it happens in both directions. I think the problem is at the point in the code where shouldMove changes to false, but I can´t find a solution. Any pointers would be much appreciated.
public class PlayerMovement2 : MonoBehaviour
{
public float speed = 5f;
public bool shouldMove = false;
Vector3 targetRight;
Vector3 targetLeft;
Rigidbody2D pRigidbody;
bool isLeft = false;
bool isRight = false;
void Start()
{
pRigidbody = GetComponent<Rigidbody2D>();
targetLeft = new Vector3(-0.25f, transform.position.y, transform.position.z);
targetRight = new Vector3(0.25f, transform.position.y, transform.position.z);
}
void Update()
{
CheckPosition();
PlsMove();
if (shouldMove && isLeft)
{
pRigidbody.MovePosition(Vector3.MoveTowards(transform.position, targetRight, speed * Time.deltaTime));
}
if (shouldMove && isRight)
{
pRigidbody.MovePosition(Vector3.MoveTowards(transform.position, targetLeft, speed * Time.deltaTime));
}
}
void PlsMove()
{
if (Input.GetMouseButtonDown(0))
{
shouldMove = true;
}
}
void CheckPosition()
{
if (transform.position.x == targetLeft.x)
{
isLeft = true;
isRight = false;
shouldMove = false;
}
else if (transform.position.x == targetRight.x)
{
isRight = true;
isLeft = false;
shouldMove = false;
}
}
}
Answer by JPhilipp · Feb 13, 2020 at 12:27 PM
Here's a streamlined version that works. I've used Rigidbody 3D for the purpose of the test, but you can convert it back to 2D. Note how in your code, for starters things are stuck at the beginning because neither isRight nor isLeft would be true in the beginning, yet at least one of them is required for any movement. Note also how isLeft and isRight in your code are mutually exclusive, so you may want to prefer a code relationship that expresses that. In below case, the target
is always exactly one side (or null), and if no target is defined it falls back on at least one of them. I'm also using FixedUpdate() to apply the physics changes to avoid jerky visuals.
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float speed = 15f;
Vector3 targetLeft;
Vector3 targetRight;
Rigidbody body;
Vector3? target = null;
void Start()
{
body = GetComponent<Rigidbody>();
targetLeft = new Vector3(-1f, transform.position.y, transform.position.z);
targetRight = new Vector3(1f, transform.position.y, transform.position.z);
}
void Update()
{
if (Input.GetMouseButtonDown(0) && target == null)
{
target = transform.position == targetLeft ? targetRight : targetLeft;
}
}
void FixedUpdate()
{
if (target != null)
{
Vector3 newPosition = Vector3.MoveTowards(
transform.position, (Vector3) target, speed * Time.fixedDeltaTime);
body.MovePosition(newPosition);
if (newPosition == (Vector3) target) { target = null; }
}
}
}
Thank you!, I just started learning and this helps a lot
Your answer
![](https://koobas.hobune.stream/wayback/20220612223240im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Y-axis movement stuck. Help me please :c,Not Moving by pressing Button :C. Help me please 0 Answers
player tilting left or right on key press / while turning left or right? 0 Answers
2D Top-Down Character: How to make my character move using physics? 1 Answer
How to make a turn like the Temple Run and NO THING ? 2 Answers
Rigidbody2D.MovePosition() doesn't do anything (but animations and transform.Translate() works) 0 Answers