Space 射击玩家碰撞无法正常工作

Space Shooter player colision not working properly

我正在尝试在 unity 官方网站页面上以 unity 的方式完成教程,但我有一些疑问,因为教程有点旧,而且他的做法并不正确,所以我已经设置了播放器和背景,现在我必须编写脚本让玩家移动并检查 x 和 z 轴之间的碰撞,所以我所做的基本上是使用 Math.clamp 方法,但是当我将 x 轴设置为例如,对于最小 -6 和最大 6,它只是在 -1 和 1 之间移动,同样发生在 z 轴上,我不知道为什么会这样:S

这里是代码

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

private Rigidbody rb;
public float velocity;
public float xMin, xMax, zMin, zMax;

void Start () {
    rb = GetComponent<Rigidbody> ();
    velocity = 3.0f;
    xMin = -6.0f;
    xMax = 6.0f;
    zMin = -4.0f;
    zMax = 7.0f;
}

// Update is called once per frame
void FixedUpdate () {
    float xAxis = Input.GetAxis ("Horizontal");
    float zAxis = Input.GetAxis ("Vertical");
    rb.velocity = new Vector3 (xAxis, 0.0f, zAxis) * velocity;
    rb.position = new Vector3 (Mathf.Clamp (xAxis, xMin, xMax), 0.0f, Mathf.Clamp (zAxis, zMin, zMax));
}

}

Mathf.Clamp sets the bounds for you values. So it can't go more or less than these. if you want the values to go further than -6 and +6, Just increase xMin, xMax, zMin, zMax values from inspector. And Input.GetAxis 的范围在 -1 到 +1 之间,因此您不会比这更远。因此,请尝试将 xAxis 和 zAxis 值乘以某个大数。例如 10。

void FixedUpdate () 
{
    float xAxis = Input.GetAxis ("Horizontal") * 10;
    float zAxis = Input.GetAxis ("Vertical") * 10;
    rb.velocity = new Vector3 (xAxis, 0.0f, zAxis) * velocity;
    rb.position = new Vector3 (Mathf.Clamp (xAxis, xMin, xMax), 0.0f, Mathf.Clamp (zAxis, zMin, zMax));
}