0
\$\begingroup\$

I am currently making top-down space shooter and would like to hear some suggestions on how to restrict player movement without causing jitters. Everything works fine except one thing: the player (spaceship) jitters back and forth when trying to move beyond the game bounds. Here is a short video to show you what I mean: https://drive.google.com/file/d/1ZsBHnSakQYh0J5srJC_KUbB00OSSQFA9/view?usp=sharing And here is a script:

using System.Collections.Generic;
using UnityEngine;

public class Starship : MonoBehaviour
{
    private float moveX, moveY;
    private float boundsY = 450f, boundsX = 850f;
    
    [SerializeField]
    private float moveSpeed;
    
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        moveX = Input.GetAxis("Horizontal");
        moveY = Input.GetAxis("Vertical");
        Vector3 currentPosition = transform.localPosition;
        transform.localPosition += new Vector3(moveX, moveY) * moveSpeed * Time.deltaTime;
        if (currentPosition.y < -boundsY)
            transform.localPosition = new Vector3(currentPosition.x, -boundsY);
        else if (currentPosition.y > boundsY)
            transform.localPosition = new Vector3(currentPosition.x, boundsY);
        else if (currentPosition.x < -boundsX)
            transform.localPosition = new Vector3(-boundsX, currentPosition.y);
        else if (currentPosition.x > boundsX)
            transform.localPosition = new Vector3(boundsX, currentPosition.y);
    }
}
\$\endgroup\$
0

2 Answers 2

1
\$\begingroup\$

So I decided to use rigidbody and some box colliders to restrict player movement. Overall I think it's the best solution for this case. I used Box colliders 2D as bounds and to detect a ship of a player. I also set Rigidbody2D for a ship. It's also important setting continuous collision detection in Rigidbody properties for the ship of a player, so the ship doesn't bounce off the bounds.

private Rigidbody2D playerRB;

void Start()
    {
        playerRB = GetComponent<Rigidbody2D>();
    }

void Update()
    {
        moveX = Input.GetAxis("Horizontal");
        moveY = Input.GetAxis("Vertical");
        playerRB.MovePosition(playerRB.position + new Vector2(moveX, moveY) * 
        Time.deltaTime * moveSpeed);
    }
\$\endgroup\$
1
  • \$\begingroup\$ The code is cleaner, easier to read and as a bonus, you can have as well obstacles where the player can't go or you are not limited by a simple box as bounds. \$\endgroup\$
    – Zibelas
    Jan 28, 2022 at 20:19
0
\$\begingroup\$

The fastest fix would be to first check if your new position is out of bounds before clipping it to the bound in case it is bigger.

void Update() {
    moveX = Input.GetAxis("Horizontal");
    moveY = Input.GetAxis("Vertical");
    Vector3 currentPosition = transform.localPosition;
    Vector3 futurePosition = currentPosition + new Vector3(moveX, moveY) * moveSpeed * Time.deltaTime;
    if (futurePosition.y < -boundsY)
        futurePosition = new Vector3(futurePosition.x, -boundsY);
    else if (futurePosition.y > boundsY)
        futurePosition = new Vector3(futurePosition.x, boundsY);
    if (futurePosition.x < -boundsX)
        futurePosition = new Vector3(-boundsX, futurePosition.y);
    else if (futurePosition.x > boundsX)
        futurePosition = new Vector3(boundsX, futurePosition.y);
    transform.localPosition = futurePosition;
}

Keep in mind that setting the position directly ignores physics. If you moved your ship with a rigidbody and had some borders (invisible or not), it would not jitter against them.

\$\endgroup\$
2
  • \$\begingroup\$ Interesting solution, however this makes the ship "sticky" i.e. if I want to go past right bounds and simultaniously press right and up arrow the ship will just stay in place unless I depress the right arrow. So I made bounds using box colliders and attached a rigidbody to a ship. Had some headaches with it too, but in the end it worked out exactly as I wanted to. \$\endgroup\$ Jan 28, 2022 at 17:53
  • \$\begingroup\$ rigidbody is overall making your life as well easier in the long run. You can always post your own solution as answer as well \$\endgroup\$
    – Zibelas
    Jan 28, 2022 at 18:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .