Reputation: 1300
I'm learning unity, and I'm trying to recreate a game of my in Unity from XNA.
I'm following this Tutorial Playlist from unity on youtube, and I've used the GameManager and BoardManager to create my map.
This is my inspector on the wall prefabs
And this is the inspector on my Player prefab
The code for the PlayerMovement
script
using UnityEngine;
namespace Assets.Scripts
{
public enum Directions
{
Back,
Left,
Front,
Right,
Idle = -1
}
public class PlayerMovement : MonoBehaviour
{
#region Public Members
public float speed;
#endregion
#region Constants
private const float DECAY_FACTOR = 0.85f;
private const float SPEED_FACTOR = 20000f;
#endregion
#region Private Members
private Rigidbody2D rb2D;
private Vector2 velocity;
private Animator animator;
#endregion
#region Game Loop Methods
private void Awake()
{
animator = GetComponent<Animator>();
rb2D = GetComponent<Rigidbody2D>();
}
private void Update()
{
float vertical = Input.GetAxisRaw("Vertical");
float horizontal = Input.GetAxisRaw("Horizontal");
UpdateVelocity(vertical, horizontal);
UpdateAnimation();
UpdateMovment();
}
#endregion
#region Animation Methods
private void UpdateAnimation()
{
Directions direction;
if (velocity.y > 0)
direction = Directions.Back;
else if (velocity.y < 0)
direction = Directions.Front;
else if (velocity.x > 0)
direction = Directions.Right;
else if (velocity.x < 0)
direction = Directions.Left;
else
direction = Directions.Idle;
SetDirection(direction);
}
private void SetDirection(Directions value)
{
animator.SetInteger("Direction", (int)value);
}
#endregion
#region Movement Methods
private void UpdateMovment()
{
Debug.Log(string.Format("HOR - {0} : VER - {1} : DIR - {2}", velocity.x, velocity.y, animator.GetInteger("Direction")));
transform.Translate(velocity.x, velocity.y, 0f, transform);
ApplySpeedDecay();
}
private void UpdateVelocity(float vertical, float horizontal)
{
if (vertical != 0)
velocity.y += Mathf.Abs(speed) / SPEED_FACTOR;
if (horizontal != 0)
velocity.x += Mathf.Abs(speed) / SPEED_FACTOR;
}
private void ApplySpeedDecay()
{
// Apply speed decay
velocity.x *= DECAY_FACTOR;
velocity.y *= DECAY_FACTOR;
// Zerofy tiny velocities
const float EPSILON = 0.01f;
if (Mathf.Abs(velocity.x) < EPSILON)
velocity.x = 0;
if (Mathf.Abs(velocity.y) < EPSILON)
velocity.y = 0;
}
#endregion
}
}
Here's an example of my problem ingame:
As you can see, the player can simply move in and out of walls as if they don't have box collider.
While writing this post, I've noticed that if I give the wall prefabs a Rigidbody2D (with Is Kinetic
left false) there's collision but the boxes move, which is the opposite of my intent. When I check Is Kinetic
, there's not collision again.
Upvotes: 2
Views: 991
Reputation: 1300
Solved - Player shouldn't be kinematic. IsKinematic should be unchecked, and the gravity in Rigidbody2D should be set to 0. Thanks guys. Gonna move on to the next collision bug now :)
Upvotes: 0
Reputation: 81
Edit-Your player has 'iskinematic' checked! I believe this is your problem!
By the unity docs "If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore." -http://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html
1.) Make sure the 'wall' has a Rigidbody2D and Rigidbody2D.isKinematic is checked
2.) Check your collision matrix. I noticed that 'player' has a layer 'BlockingLayer'. (You'll probably want to change that) But if in Edit->Project Settings->Physics2D the 'BlockingLayer' x 'BlockingLayer' checkbox is unchecked, then that would explain the lack of collisions.
Upvotes: 2