Question by
DerAntagonist · Aug 19, 2018 at 06:34 PM ·
2drigidbodycollidertile
Why are the 2D colliders not working?
I created a 2D Tilemap with forest and attached a tilemap collider to the trees. Nevertheless the rigidbody with a capsule collider runs straight over the trees. The only code I used is the following to make the player move:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
Direction currentDir;
Vector2 input;
bool isMoving = false;
Vector3 startPos;
Vector3 endPos;
float t;
public Sprite northSprite;
public Sprite eastSprite;
public Sprite southSprite;
public Sprite westSprite;
public float walkSpeed = 3f;
public bool isAllowedToMove = true;
void Start()
{
isAllowedToMove = true;
}
void Update()
{
if (!isMoving && isAllowedToMove)
{
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
input.y = 0;
else
input.x = 0;
if (input != Vector2.zero)
{
if (input.x < 0)
{
currentDir = Direction.West;
}
if (input.x > 0)
{
currentDir = Direction.East;
}
if (input.y < 0)
{
currentDir = Direction.South;
}
if (input.y > 0)
{
currentDir = Direction.North;
}
switch (currentDir)
{
case Direction.North:
gameObject.GetComponent<SpriteRenderer>().sprite = northSprite;
break;
case Direction.East:
gameObject.GetComponent<SpriteRenderer>().sprite = eastSprite;
break;
case Direction.South:
gameObject.GetComponent<SpriteRenderer>().sprite = southSprite;
break;
case Direction.West:
gameObject.GetComponent<SpriteRenderer>().sprite = westSprite;
break;
}
StartCoroutine(Move(transform));
}
}
}
public IEnumerator Move(Transform entity)
{
isMoving = true;
startPos = entity.position;
t = 0;
endPos = new Vector3(startPos.x + System.Math.Sign(input.x), startPos.y + System.Math.Sign(input.y), startPos.z);
while (t < 1f)
{
t += Time.deltaTime * walkSpeed;
entity.position = Vector3.Lerp(startPos, endPos, t);
yield return null;
}
isMoving = false;
yield return 0;
}
}
enum Direction
{
North,
East,
South,
West
}
,I created a Tilemap and attached the tilemap collider 2D to the forest. Nevertheless the Rigidbody with a Capsule Collider runs straight over the trees. The only code I have is the following to move my charakters. Why arent the colliders working properly?
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
Direction currentDir;
Vector2 input;
bool isMoving = false;
Vector3 startPos;
Vector3 endPos;
float t;
public Sprite northSprite;
public Sprite eastSprite;
public Sprite southSprite;
public Sprite westSprite;
public float walkSpeed = 3f;
public bool isAllowedToMove = true;
void Start()
{
isAllowedToMove = true;
}
void Update()
{
if (!isMoving && isAllowedToMove)
{
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
input.y = 0;
else
input.x = 0;
if (input != Vector2.zero)
{
if (input.x < 0)
{
currentDir = Direction.West;
}
if (input.x > 0)
{
currentDir = Direction.East;
}
if (input.y < 0)
{
currentDir = Direction.South;
}
if (input.y > 0)
{
currentDir = Direction.North;
}
switch (currentDir)
{
case Direction.North:
gameObject.GetComponent<SpriteRenderer>().sprite = northSprite;
break;
case Direction.East:
gameObject.GetComponent<SpriteRenderer>().sprite = eastSprite;
break;
case Direction.South:
gameObject.GetComponent<SpriteRenderer>().sprite = southSprite;
break;
case Direction.West:
gameObject.GetComponent<SpriteRenderer>().sprite = westSprite;
break;
}
StartCoroutine(Move(transform));
}
}
}
public IEnumerator Move(Transform entity)
{
isMoving = true;
startPos = entity.position;
t = 0;
endPos = new Vector3(startPos.x + System.Math.Sign(input.x), startPos.y + System.Math.Sign(input.y), startPos.z);
while (t < 1f)
{
t += Time.deltaTime * walkSpeed;
entity.position = Vector3.Lerp(startPos, endPos, t);
yield return null;
}
isMoving = false;
yield return 0;
}
}
enum Direction
{
North,
East,
South,
West
}
Comment