【发布时间】:2016-09-23 21:28:15
【问题描述】:
我在 Unity 中有一个盒子,后面跟着一个飞机上的相机。我正在尝试处理盒子和不同物体之间的碰撞。当它与不同的东西碰撞时,它会旋转、跳跃并发生奇怪的事情。我向 YouTube 上传了一个视频来显示问题。 The video.
我创建了一个带有相机和盒子的 empty。这个空的刚体的质量为 1。
空有脚本组件:
using UnityEngine;
using System.Collections;
public class Character : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision collision)
{
Debug.Log ("Entered OnCollisionEnter function");
if (collision.gameObject.name == "Wall") {
GetComponent<Rigidbody>().velocity = Vector3.zero;
Debug.Log ("Inside if statement");
}
}
}
如你所见,我尝试编写一个停止立方体移动的代码来处理碰撞。
可以帮助你们的其他信息:
盒子
它有一个盒子对撞机。脚本:
using UnityEngine;
using System.Collections;
public class MoveCharacter : MonoBehaviour {
public float deltaMovement = 10f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Moving();
}
void Moving()
{
//Moves the character to where it needs.
if (Input.GetKey (KeyCode.A)) {
transform.Translate (new Vector3 (-deltaMovement, 0f, 0f) * Time.deltaTime);
} else if (Input.GetKey (KeyCode.D)){
transform.Translate (new Vector3 (deltaMovement, 0f, 0f) * Time.deltaTime);
}
float yRotation = Camera.main.transform.eulerAngles.y;
float movementX = Mathf.Sin ((yRotation * Mathf.PI) / 180) * deltaMovement;
float movementZ = Mathf.Cos ((yRotation * Mathf.PI) / 180) * deltaMovement;
if (Input.GetKey (KeyCode.W)) {
transform.Translate (new Vector3 (movementX, 0f, movementZ) * Time.deltaTime, Space.World);
} else if (Input.GetKey (KeyCode.S)){
transform.Translate (new Vector3 (-movementX, 0f, -movementZ) * Time.deltaTime, Space.World);
}
}
}
墙
这是一个带有网格对撞机的飞机,有无刚体没有区别,同样的问题......
有什么帮助吗?
【问题讨论】:
标签: c# unity3d collision-detection gameobject rigid-bodies