团结:对角线运动问题

时间:2018-02-11 21:09:20

标签: unity3d unity5 unity3d-2dtools

我有一个测试2D游戏,我的玩家从左向右移动,当他到达屏幕的末端时,它只会在另一侧进行转换。我改变了主意,让我的球员对角线移动。它确实有效,但我不知道如何让播放器在屏幕结束时停止。我不希望它在另一边转变,而是停止。到目前为止,我所有的结果都是边缘有些毛刺或根本没有停止。我提供了我的PlayerController脚本。现在我的玩家对角线移动,他将继续在屏幕边缘后继续。如果有人可以帮助我,我将非常感激。我从未想过我会处理对角线运动,但我真的想学习如何去做。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class PlayerController : MonoBehaviour {

    public float speed = 7;
    public float speedy = 7;

    public event System.Action OnPlayerDeath;
    float screenHalfWidthInWorldUnits;
    Rigidbody2D rb;

    void Start () {
        rb = GetComponent<Rigidbody2D>();
        float halfPlayerWidth = transform.localScale.x / 2f;
        screenHalfWidthInWorldUnits = Camera.main.aspect * Camera.main.orthographicSize;
    }

    void Update()
    {
        float inputX = Input.GetAxisRaw("Horizontal");
        float velocity = inputX * speed;
        transform.Translate(Vector2.right * velocity * Time.deltaTime); 
    }


    public void MoveRight()
    {
        rb.velocity = new Vector2(speed, speedy);
    }

    public void MoveLeft()
    {
        rb.velocity = new Vector2(-speed, -speedy);
    }


    public void Stop()
    {
        rb.velocity =  Vector2.zero;
    }

    void OnTriggerEnter2D(Collider2D triggerCollider)
    {
        if (triggerCollider.tag =="Box")
        {
            if (OnPlayerDeath != null)
            {
                OnPlayerDeath();
            }
            Destroy(gameObject);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以检查播放器是否位于您定义为地图边框的位置。

如果分别检查x轴和y轴,则可以将x或y轴锁定到边框而不是更远。

以下是我之前制作的脚本示例。

如果我理解正确,你希望能够对角移动。在我下面的示例脚本中,您可以直接和对角移动,也可以在边缘之间扭曲,或者在您想要的时候停在边缘。

这个脚本比你需要的更先进,所以如果有什么事情让你感到困惑,请告诉我。

请注意,如果您将布尔_ShouldWarp设置为false,他将停在边框处,否则他将从地图的边缘到边缘扭曲。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    public float _Speed = 5f;
    public WorldBounds _WorldBounds;
    public bool _ShouldWarp; //If _ShouldWarp is false, will block players movement instead.

    void Update()
    {
        Move();
        WarpIfAtBoundary();
    }

    void Move()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        transform.Translate(Vector3.right * Time.deltaTime * _Speed * horizontal);
        transform.Translate(Vector3.up * Time.deltaTime * _Speed * vertical);
    }

    void WarpIfAtBoundary()
    {
        //X Axis
        //If player is at positive X boundary
        if (transform.position.x > (_WorldBounds.xPlus + _WorldBounds.xBuffer))
        {
            if (_ShouldWarp) //Teleport/warp player is set to enabled
            {
                transform.position = new Vector3(_WorldBounds.xMinus, transform.position.y, transform.position.z);
            }
            else //Otherwise keep player in border position
            {
                transform.position = new Vector3(_WorldBounds.xPlus + _WorldBounds.xBuffer, transform.position.y, transform.position.z);
            }
        }
        //Else if player is at negative X boundary
        else if (transform.position.x < (_WorldBounds.xMinus - _WorldBounds.xBuffer))
        {
            if (_ShouldWarp)//Teleport/warp player is set to enabled
            {
                transform.position = new Vector3(_WorldBounds.xPlus, transform.position.y, transform.position.z);
            }
            else //Otherwise keep player in border position
            {
                transform.position = new Vector3(_WorldBounds.xMinus - _WorldBounds.xBuffer, transform.position.y, transform.position.z);
            }
        }

        //Y Axis
        //If player is at positive Y boundary
        if (transform.position.y > (_WorldBounds.yPlus + _WorldBounds.yBuffer))
        {
            if (_ShouldWarp)//Teleport/warp player is set to enabled
            {
                transform.position = new Vector3(transform.position.x, _WorldBounds.yMinus, transform.position.z);
            }
            else //Otherwise keep player in border position
            {
                transform.position = new Vector3(transform.position.x, _WorldBounds.yPlus + _WorldBounds.yBuffer, transform.position.z);
            }
        }
        //Else if player is at negative Y boundary
        else if (transform.position.y < (_WorldBounds.yMinus - _WorldBounds.yBuffer))
        {
            if (_ShouldWarp)//Teleport/warp player is set to enabled
            { 
                transform.position = new Vector3(transform.position.x, _WorldBounds.yPlus, transform.position.z);
            }
            else //Otherwise keep player in border position
            {
                transform.position = new Vector3(transform.position.x, _WorldBounds.yMinus - _WorldBounds.yBuffer, transform.position.z);
            }
        }
    }
}

//Set as serializable so it displays correctly in Unity's inspector window.
[System.Serializable]
public class WorldBounds
{
    [Header("Bounds")]
    public float xMinus = -9.4f;
    public float xPlus = 9.4f;
    public float yMinus = -9.4f;
    public float yPlus = 9.4f;
    [Header("BufferZone")]
    public float xBuffer = 1f;
    public float yBuffer = 1f;
}

编辑:

  

通过添加,我可以将动作分配给我的两个按钮。一个是向上和向右,另一个是向下和向左。

        void Move()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        transform.Translate(Vector3.right * Time.deltaTime * _Speed * horizontal);
        transform.Translate(Vector3.up * Time.deltaTime * _Speed * horizontal);
    }

这应该可以左右对角地向上和向下移动。

我做的改变是使用水平输入进行X和Y移动。

  

我不需要扭曲。只是步入已定义的边界

您可以将Warp布尔值设置为false或从代码中删除扭曲部分:-),应该可以。