K
K
Kt0T02020-09-05 10:15:20
Unity
Kt0T0, 2020-09-05 10:15:20

How to prevent the character from moving outside the camera?

using UnityEngine;

public class PlayerControll : MonoBehaviour
{

    public float horizontalSpeed;
    public float speedX;
    public void MoveLeft()
    {
        speedX = -horizontalSpeed;
    }

    public void MoveRight()
    {
        speedX = horizontalSpeed;
    }
    public void MoveStop()
    {
        speedX = 0;
    }
    void FixedUpdate()
    {
        transform.Translate(speedX, 0, 0);
    }
}


Borders
5f533af386f28895693514.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Rezanov, 2020-09-05
@Kt0T0

If he touches one of the barriers with the borders (extreme points), forbid him to move. Every time a player moves to the right or left, check if he has gone out of bounds, if he starts to go out, then prohibit movement in that direction.

Z
zZaKko, 2020-09-06
@zZaKko

If the object crosses the border positions, return the character to the border position.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question