Reputation: 54212
I have a simple scene in 2D. The right yellow box is the "Player", while the green & brown thing is the "Obstacle".
Player has a BoxCollider2D
, RigidBody2D
and a C# script named Hero.cs
attached to it. BoxCollider2D
enabled Is Trigger
; RigidBody2D
enabled Is Kinematics
; other settings are left in default values.
Obstacle has only a BoxCollider2D
with Is Trigger
enabled.
and here is the 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");
}
}
Only "Triggering" appears in Console Log.
My question is: What should I add to make the "Player" inaccessible to the "Obstacle" (no need to bounce away)?
Note: Using Unity 4.5
Update: After I set Gravity Scale
to 0, collision detection works, but in a strange way. The "Player" go sideway during collision. Watch this YouTube video for action.
I expect the Player only go along X or Y axis. What did I miss ?
Upvotes: 0
Views: 2133
Reputation: 3234
Triggers let other colliders pass through without any collision happening. They are only triggering an event, hence the name.
If you disable IsTrigger
for both objects, you will get collisions and the corresponding events are fired.
More infos here: http://docs.unity3d.com/Manual/CollidersOverview.html
Kinematic rigidbody colliders will only collide with other non-kinematic rigidbody colliders. Have a look at this matrix http://docs.unity3d.com/Manual/CollisionsOverview.html
Disable IsKinematic
and move the player with MovePosition
if you don't want to use force values to move the player.
Upvotes: 2