我正在使用触发器对撞机(带有触发器的circleCollider2D),但它只能感知与某些物体的碰撞。
这是触发器上的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Scoring : MonoBehaviour {
public updateScore updateScore;
Transform earth;
// Use this for initialization
void Start () {
earth = GetComponent<Transform>();
}
void OnTriggerExit2D(Collider2D other)
{
Debug.Log("other collider " + other);
updateScore.score++;
}
}
它正在感知这个对象:
它是一个2D流星对象,上面有一个rigidbody2D和一个circleCollider2D。 它附有这个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CometMovement : MonoBehaviour {
public float speed = 0.5f;
Rigidbody2D rb;
Vector2 direction;
// Use this for initialization
void Start () {
direction = new Vector2(Random.Range(10.0f, -10.0f), Random.Range(10.0f, -10.0f));
rb = GetComponent<Rigidbody2D>();
rb.AddForce(direction * speed);
}
}
但它没有感知到这个下面的对象。我需要它来感知下面这个对象:
它是一个具有rigidbody2d和CircleCollider2D的地球对象,与流星对象几乎相同。 它附有这个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EarthController : MonoBehaviour {
public float force = 0.5f;
Rigidbody2D rb;
Vector2 direction;
Transform transform;
// Use this for initialization
void Start () {
direction = new Vector2(0, 4);
transform = GetComponent<Transform>();
rb = GetComponent<Rigidbody2D>();
rb.isKinematic = false;
}
// void OnCollisionEnter2D(Collision2D coll) {
// SceneManager.LoadScene("Level 01");
//
// }
// Update is called once per frame
void Update () {
if(Input.GetButton("Fire1")){
rb.isKinematic = false;
if(transform.position.x <= 0 && transform.position.y <= 4){
direction = new Vector2(0, -4);
} else {
direction = new Vector2(0, 4);
}
rb.AddForce(direction * force);
Debug.Log("going this fast: " + rb.inertia);
} else {
rb.isKinematic = true;
rb.velocity = Vector2.zero;
}
}
}
答案 0 :(得分:3)
如果启用了isKinematic,则强制,碰撞或关节将不再影响刚体
if(Input.GetButton("Fire1")){
rb.isKinematic = false;
// ...
} else {
rb.isKinematic = true;
}
另外,请检查您的Physics parameters?
确保两个碰撞器都不是触发器。不确定这个。