Chris W.
Chris W.

Reputation: 21

Unity C# Movement based on rotation?

I'm currently working with a grid in Unity 3D and am having trouble keeping my player on the grid. My movement script works as so:

  1. When 'w' is pressed, rb.position += rb.rotation * Vector3.forward * 10;
  2. posX = rb.position.x / 10, posY = rb.position.z / 10

My current problem is I haven't found the solution for how to keep my player from going off the board. This is especially difficult because based off of the rotation of the player, 'w' can either add +/- 1 to x, or +/- to y. This will be the same for moving left, right, and back as well, all values based on rotation.

I also plan on reworking the movement system to automatically snap the player to the value of variables posX and posY. The only way I can think to program the system would be simply hardcoding it (if (rotation = 0) {posX + 1}, if (rotation = 90) {posY + 1}, if (rotation = 180) {posX- 1}, if (rotation = 270) {posY - 1}). Very inefficient.

Any thoughts on how to get around this?

    public class Movement : MonoBehaviour
{   bool Rotating = false;
    bool Moving = false;
    public Rigidbody rb;
    private Quaternion endRotation;
    [SerializeField] private AnimationCurve curve;
    private Quaternion startRotation;
    private Vector3 startPosition;
    public GameObject Grid;
    private Vector3 endPosition;
    public int xRange;
    public int yRange;
    public float posX;
    public float posY;
    public float desiredDuration = 1f;
    private float elapsedTime;
    float percentageComplete = 0f;  

    private void Awake(){
        rb = GetComponent<Rigidbody>();
        var grid = Grid.GetComponent<GridCreation>();
        xRange = grid.width;
        yRange = grid.height;
    }

    public void TurnRight(InputAction.CallbackContext context){ 
            
            if (context.started && Rotating == false && Moving == false) {
                percentageComplete = 0;
                startRotation = rb.rotation;
                endRotation = startRotation * Quaternion.Euler(0,90,0);
                Rotating = true;
                elapsedTime = 0;
            }
            }
    public void TurnLeft(InputAction.CallbackContext context){ 
            
            if (context.started && Rotating == false && Moving == false) {
                percentageComplete = 0;
                startRotation = rb.rotation;
                endRotation = startRotation * Quaternion.Euler(0,-90,0);
                Rotating = true;
                elapsedTime = 0;
            }}
    //MOVING BASED ON ROTATION//
    public void Forward(InputAction.CallbackContext context){ 
            
            if (context.started && Rotating == false && Moving == false) {
                    percentageComplete = 0;
                    startPosition = rb.position;
                    endPosition += rb.rotation * Vector3.forward * 10;
                    endPosition[1] = 1;
                    Moving = true;
                    elapsedTime = 0;
                    ;
            }}
            
    public void Update() { 
        if (Rotating == true){
            if (percentageComplete < 1) {
                elapsedTime += Time.deltaTime;
                percentageComplete = elapsedTime / desiredDuration;
                rb.rotation = Quaternion.Lerp(startRotation, endRotation, curve.Evaluate(percentageComplete));
            }
                       
            if (percentageComplete > 1) {
                Rotating = false;}
                            }
        if (Moving == true){
            if (percentageComplete < 1) {
                elapsedTime += Time.deltaTime;
                percentageComplete = elapsedTime / desiredDuration;
                rb.position = Vector3.Lerp(startPosition, endPosition, curve.Evaluate(percentageComplete));
            if (percentageComplete > 1) {
                Moving = false;
                // returns players current position on board
                posX = Mathf.Round(rb.position.x / 10);
                posY = Mathf.Round(rb.position.z / 10);}
        }

    }
        }}

Upvotes: 0

Views: 338

Answers (1)

Pretzl
Pretzl

Reputation: 28

Hey I think you could just use Colliders to keep the player from falling off the board! Just add Empty GameObjects arround the Board and give them Colliders, when the player bumps into the Collider it works like an invisible wall and keeps the player from going further. You could even add Material to the Collider make the player bounce off the invisible wall if you need that. Using a BoxCollider would be best I think since it probably fits best arround your grid. I hope I was able to help you out! :)

Upvotes: 2

Related Questions