- Home /
2d collision not working?
Hey all,
I've got two game objects, each with a rigidbody2d and a box collider2d. Set to kinematic, both on a default layer. But for the life of me I can't get them to collide with each other.
'MovingObject' script below, attached to the Photon (the movable game object). The other object, 'Wall', doesn't have an attached script (although I have tried it that way around — and also tried giving it to both) If anyone could give me some pointers I'd really appreciate it... :/
edit: Maybe I should add that if I untick 'is kinematic' for the 'photon' and play the game it falls on top of the wall and collides with it (it bounces continuously after collision).
using UnityEngine;
using System.Collections;
public class MovingObject : MonoBehaviour {
public GameObject Photon;
Vector3 pos; // For movement
float speed = 5f; // Speed of movement
void OnCollisionEnter2d (Collision2D col)
{
Destroy(col.gameObject);
}
// Use this for initialization
void Start () {
pos = transform.position; // Take the initial position
}
// Update is called once per frame
void FixedUpdate () {
if(Input.GetKey("up") && transform.position == pos) {
pos += Vector3.up;
}
if(Input.GetKey("down") && transform.position == pos) {
pos += Vector3.down;
}
if(Input.GetKey("left") && transform.position == pos) {
pos += Vector3.left;
}
if(Input.GetKey("right") && transform.position == pos) {
pos += Vector3.right;
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed); // Move there
}
}
Your answer
Follow this Question
Related Questions
[2D] Getting particles to collide with a game object(Explosion debris effect) 0 Answers
Make 2D projectile collide only from the outside 1 Answer
Smooth 2D collisions / colliders pushbacks 0 Answers
Best way to manually calculate collision between GameObjects and a Tilemap without using physics 1 Answer