Hello Guest

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - attak

Pages: [1]
1
Support / Re: Beginning Unity and 2D Toolkit
« on: February 09, 2013, 03:57:57 am »
I'm working through the same book. Very helpful.

2
Support / Re: Can't get animated sprite's OnCollisionEnter to trigger
« on: January 06, 2013, 05:50:31 am »
thanks for the quick response. I'll try it out :)

3
Support / Can't get animated sprite's OnCollisionEnter to trigger
« on: January 05, 2013, 07:43:26 pm »
Preface: I'm new to Unity and 2D Toolkit. I have experience with C#/XNA, but I started messing with Unity and 2D Toolkit this week. Excuse me if this is a dumb question :)


I am using the tk2dCamera (size 480) and I have two sprite collections setup (Player and Enemy), that are using two different sprite sheets for their animations.

In the Player and Enemy's sprite collection, I have each sprite from the sprite sheet setup with a Box Custom (using defaults) for the Collider type with a "Black Zero Alpha Pad" method. The collider depth of each is 0.1 and they have "use tk2dCamera" checked.

Both the Player and Enemy Animated Sprite are using a RigidBody (no gravity) that have "Is Kinematic" checked. "Create collider" is also checked for both Animated Sprites. The Box Collider for each Animated Sprite has "Is Trigger" checked, and uses the defaults for the rest.

Right now, the Enemy does not move and the Player is controlled using WASD and plays an animation depending on the direction it moves. The movement and animation of the Player works fine.

I'm using this as the C# script as the Player's "PlayerController.cs":

Code: [Select]
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
tk2dAnimatedSprite sprite;
bool walking = false;
bool isColliding = false;

private float moveSpeed = 50;
private int moveDirX;
private int moveDirY;
private Vector3 movement;
private Transform thisTransform;

void Start ()
{
sprite = GetComponent<tk2dAnimatedSprite>();
}

void HitCompleteDelegate(tk2dAnimatedSprite sprite, int clipId)
    {
        if (!walking)
        {
            sprite.Play("Idle");
        }
    }

void PlayIfNotPlaying(string clip)
{
if (!sprite.Playing || sprite.CurrentClip.name != clip)
{
sprite.Play(clip);
}
}

void OnCollisionEnter(Collision collision)
{
Debug.Log("OnCollisionEnter");

isColliding = true;

foreach (ContactPoint contact in collision.contacts)
{
Debug.DrawRay(contact.point, contact.normal, Color.red);
}
}

void OnCollisionExit(Collision collision)
{
Debug.Log("OnCollisionExit");

isColliding = false;
}

void OnTriggerEnter(Collider other)
{
Debug.Log("OnTriggerEnter");
}

void OnTriggerExit(Collider other)
{
Debug.Log("OnTriggerExit");
}

void Update ()
{
moveDirX = 0;
moveDirY = 0;

if (isColliding)
{
sprite.color = Color.red;
}
else
{
sprite.color = Color.white;
}

// Left
if (Input.GetKey(KeyCode.A))
{
moveDirX = -1;

// Up-Left
if (Input.GetKey(KeyCode.W))
{
moveDirY = 1;

PlayIfNotPlaying("Up");
}
// Down-Left
else if (Input.GetKey(KeyCode.S))
{
moveDirY = -1;

PlayIfNotPlaying("Down");
}
else
{
PlayIfNotPlaying("Left");
}

walking = true;
}
else if (Input.GetKeyUp(KeyCode.A))
{
walking = false;
}

// Right
if (Input.GetKey(KeyCode.D))
{
moveDirX = 1;

// Up-Right
if (Input.GetKey(KeyCode.W))
{
moveDirY = 1;

PlayIfNotPlaying("Up");
}
// Down-Right
else if (Input.GetKey(KeyCode.S))
{
moveDirY = -1;

PlayIfNotPlaying("Down");
}
else
{
PlayIfNotPlaying("Right");
}

walking = true;
}
else if (Input.GetKeyUp(KeyCode.D))
{
walking = false;
}

// Up
if (Input.GetKey(KeyCode.W))
{
moveDirY = 1;

// Up-Right
if (Input.GetKey(KeyCode.D))
{
moveDirX = 1;
}

PlayIfNotPlaying("Up");
walking = true;
}
else if (Input.GetKeyUp(KeyCode.W))
{
walking = false;
}

// Down
if (Input.GetKey(KeyCode.S))
{
moveDirY = -1;

// Down-Right
if (Input.GetKey(KeyCode.D))
{
moveDirX = 1;
}

PlayIfNotPlaying("Down");

walking = true;
}
else if (Input.GetKeyUp(KeyCode.S))
{
walking = false;
}

if (walking)
{
movement = new Vector3(moveDirX, moveDirY, 0f);
movement *= Time.deltaTime * moveSpeed;

transform.Translate(movement.x, movement.y, 0f);
}
else
{
PlayIfNotPlaying("Idle");
}
}
}

The problem I'm having is that OnCollisionEnter and OnCollisionExit are never called for the Player. The OnTriggerEnter and OnTriggerExit are called though, but I'm not sure if that is something I should use to detect collision instead of OnCollision*.

My scene is using a TileMap, but the collision doesn't happen regardless if I'm using a TileMap.

Here are screenshots of the Player and Enemy animated sprite inspectors.

Player:
https://dl.dropbox.com/u/186383/Unity/Player.png

(for some reason I don't get why the Player sprite shows up in the game screen behind the tiles, but when it is ran, it shows up correctly).

Enemy:
https://dl.dropbox.com/u/186383/Unity/Enemy.png

Edit: Removed project

Pages: [1]