【发布时间】:2014-06-10 10:59:29
【问题描述】:
我有一个简单的 2D 场景。右边的黄色盒子是“玩家”,而绿色和棕色的东西是“障碍物”。
播放器有一个BoxCollider2D、RigidBody2D 和一个名为Hero.cs 的C# 脚本。 BoxCollider2D 启用 Is Trigger; RigidBody2D 启用 Is Kinematics;其他设置保留默认值。
Obstacle 只有一个BoxCollider2D 并启用了Is Trigger。
这里是Hero.cs:
using UnityEngine;
using System.Collections;
public class Hero : MonoBehaviour {
public float moveSpeed = 0.1f;
private Vector3 moveDirection;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 currentPos = transform.position;
if(Input.GetKey("left")) {
transform.Translate(new Vector3(-1, 0, 0));
} else if(Input.GetKey("right")) {
transform.Translate(new Vector3(1, 0, 0));
}
}
void OnCollisionEnter2D(Collision2D collision) {
Debug.Log("Colliding");
}
void OnTriggerEnter2D(Collider2D other) {
Debug.Log("Triggering");
}
}
控制台日志中仅显示“触发”。
我的问题是:我应该添加什么来使“玩家”无法进入“障碍物”(无需反弹)?
注意:使用 Unity 4.5
更新:在我将 Gravity Scale 设置为 0 后,碰撞检测可以工作,但方式很奇怪。 “玩家”在碰撞过程中侧身。观看此YouTube video for action。
我希望播放器只能沿 X 或 Y 轴移动。我错过了什么?
【问题讨论】: