我是Unity的新手。
下面是我的简单字符控制器C#脚本。我使用3d立方体与盒子碰撞和刚体作为我的墙和玩家。目前,当我的玩家接触到一面墙时,它就会继续前进。
为什么我的脚本不工作?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour {
public float speed = 180;
private Rigidbody rig;
private Vector3 movement;
// Use this for initialization
void Start () {
rig = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
if (rig.velocity.magnitude <= 0)
{
if (Input.GetKeyUp("up"))
rig.velocity = new Vector3(0, 0, rig.position.z * speed );
else if (Input.GetKeyUp("down"))
rig.velocity = new Vector3(0, 0, rig.position.z * speed * -1);
else if (Input.GetKeyUp("right"))
rig.velocity = new Vector3(rig.position.x * speed, 0, 0);
else if (Input.GetKeyUp("left"))
rig.velocity = new Vector3(rig.position.x * speed * -1, 0, 0);
}
}
void OnCollisionEnter(Collision collision)
{
rig.velocity = Vector3.zero;
}
}发布于 2018-02-16 05:40:55
上面的脚本是有效的。我的y位置高于我的墙的位置,所以没有任何碰撞。我觉得自己很傻。留下帖子来提醒我的失败。
https://stackoverflow.com/questions/48816483
复制相似问题