2016-10-23 29 views
0

有2个GameObjects;一个平台和一个球。球通过平台对撞机坠落

通过自定义控制器控制球,平台通过动画进行移动。


组件

platform 
    + rigidbody 
    + box collider 

ball 
    + rigidbody 
    + sphere collider 

当球开始与平台接触,球应该停止目前的速度,实现平台它是在接触的速度。然而目前,球只是直接穿过平台,就好像没有连接对撞机一样。播放器的

代码:

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 

public class PlayerController : MonoBehaviour { 

public Text winText; 
public float speed; 
public Text countText; 
public GameObject light; 

public GameObject player; 
private Rigidbody rb; 
private int count; 
private int a = 0; 
private int b = 0; 
private int c = 0; 

void Start() 
{ 
    rb = GetComponent<Rigidbody>(); 
    count = 0; 
    SetCountText(); 
    winText.text = ""; 
} 
void FixedUpdate() 
{ 
    if (player.transform.position.y < -15) { 
     transform.position = new Vector3(a, b, c); 

    } 
    float moveHorizontal = Input.GetAxis ("Horizontal"); 
    float moveVertical = Input.GetAxis ("Vertical"); 

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); 
    rb.AddForce (movement * speed); 

} 
void OnTriggerEnter (Collider other) 
{ 
    if (other.gameObject.CompareTag ("Pick Up")) 
    { 
     other.gameObject.SetActive (false); 
     count = count + 1; 
     SetCountText(); 
    } 
    if (other.gameObject.CompareTag ("Check point")) 
    { 
     other.gameObject.SetActive (false); 
     light.gameObject.SetActive (false); 
     a = 0; 
     b = -10; 
     c = 96; 
    } 
} 
void SetCountText() 
{ 
    countText.text = "Score: " + count.ToString(); 
    if (count >= 8) 
    { 
     winText.text = "You Win!"; 
    } 
} 

}

+0

请记住,使用变换组件移动物理对象可能会导致物理不能正确应用:取决于您的球控制器可能是一个原因? – Kardux

回答

0

快速移动的物体需要动态ContinuousDynamic或连续模式工作可靠

+0

![Valid XHTML](http://imgur.com/a/KECXE)。 – Axel121

+0

![player](http://imgur.com/a/fG72z)。 – Axel121

1
  • 确保您有相同的Z所有这些游戏对象轴。
  • 在OnCollisionEnter2D()方法中抛出debug.log()消息以查看它们是否实际发生冲突。
  • 你还可以检查你正在使用的碰撞体的类型。关于冲突

更多详细信息统一: https://docs.unity3d.com/Manual/CollidersOverview.html

而且如果定制控制器确保东西没有改变球的位置去的平台下方。

3

你说你正在使用自定义控制器。请确保您没有使用Transform()手动更改球的位置并移动球,因为这违背了物理法则的统一。请使用Rigidbody.MovePosition()。 更多在Unity docs

+0

我添加了我的Player代码,所以我应该使用Rigidbody.MovePosition()。 – Axel121

+0

我的理解能力不错吗? – Axel121

+0

我认为问题出在这些线上: –

0

确保两个刚体都是动力学,而碰撞体不是触发器。多数民众赞成在真正为我工作。