Hello Guest

Author Topic: Box trimmed - Simple Collision  (Read 4601 times)

ironquark

  • Newbie
  • *
  • Posts: 14
    • View Profile
Box trimmed - Simple Collision
« on: November 17, 2015, 04:47:16 pm »
Hello,

How are you?

I'm justing trying to understand the basics of collision with 2D Toolkit.

I have two sprites and I'm able to move the player with a script, the question is... When I imported the sprite at Sprite Collection I put it as box trimmed, When a sprite get above the other, how to detect the collision event and give a command?

My code:
Code: [Select]
~using UnityEngine;
using System.Collections;

public class FishMovement : MonoBehaviour {

public float speed = 0.5f;

void OnCollisionEnter(Collision collision)
{

//if (collision.gameObject.tag == "Eletric") {
Application.Quit ();
// }
}

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
}
}


I've already added a RigidBody at the Sprite but its not working...

Greetings
« Last Edit: November 17, 2015, 05:21:15 pm by ironquark »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Box trimmed - Simple Collision
« Reply #1 on: November 17, 2015, 11:02:35 pm »
You'll need a kinematic rigid body on the moving object. You should probably use RigidBody2D.MovePosition vs transform.position. Try those and it should behave a bit better.

ironquark

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Box trimmed - Simple Collision
« Reply #2 on: November 21, 2015, 01:05:46 pm »
After putting one RigidBody for earch object now its working thank you!

I taked a look in this video fo finish my sample:
https://www.youtube.com/watch?v=xwLYy1yEerY