繁体   English   中英

限制X和Y轴上的运动

[英]Limiting Movement on X and Y axis

我想将沿X和Y轴的移动限制在-7.5和7.5之间。 最简单的方法是什么?

到目前为止,这是我的动作脚本:

using UnityEngine;
using System.Collections;

public class PlayerPaddle : MonoBehaviour {
private float speed = 0.1f;

void Start ()
{

}

void Update ()
{
    if (Input.GetKey(KeyCode.UpArrow))
        this.transform.position += Vector3.up * speed;
    if (Input.GetKey(KeyCode.DownArrow))
        this.transform.position += Vector3.down * speed;
    if (Input.GetKey(KeyCode.LeftArrow))
        this.transform.position += Vector3.left * speed;
    if (Input.GetKey(KeyCode.RightArrow))
        this.transform.position += Vector3.right * speed;   
}
}

您将要确保当前位置加上下一步动作不会超出您的范围。

您可能需要查阅一些有关碰撞检测的文献。 这是一个例子:

例:

if (Input.GetKey(KeyCode.LeftArrow))
{
   if((this.transform.position + (Vector3.left * speed)) > -7.5)
      this.transform.position += Vector3.left * speed;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM