Issue with character movement and collision using Physics2D.Raycast.
Hey guys, I've gotten this bug multiple times in the past trying to create a snap-movement game.
In the shot bellow you can see that the character is at (-7, 0) and attempting to move to (-6, 0).
This is my current script to handle movement:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
Vector3 pos;
public float speed = 1.5f;
private int collisionLayers = 7 << 8;
void Start () {
pos = transform.position;
}
void FixedUpdate () {
if(Input.GetKey(KeyCode.A) && transform.position == pos) {
CheckCollision(pos + Vector3.left);
}
if(Input.GetKey(KeyCode.D) && transform.position == pos) {
CheckCollision(pos + Vector3.right);
}
if(Input.GetKey(KeyCode.W) && transform.position == pos) {
CheckCollision(pos + Vector3.up);
}
if(Input.GetKey(KeyCode.S) && transform.position == pos) {
CheckCollision(pos + Vector3.down);
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
}
void CheckCollision(Vector3 destination){
Debug.Log (transform.position);
Debug.Log (destination);
RaycastHit2D hit = Physics2D.Raycast (transform.position, destination, 1f, collisionLayers);
if (hit.collider == null) {
pos = destination;
} else {
Debug.Log (hit.collider.gameObject + ": (" + hit.collider.transform.position.x + ", " + hit.collider.transform.position.y + ")");
}
}
};
Expected response:
Using Raycasting2D a line is drawn between one point and another, if the line comes into contact with any colliders it can be saved as a 'hit'.
When drawing a line from the character's position (-7, 0) to (-6,0) there should be no collision taking place.
The character may then continue movement to (-6, 0) "one tile to the right"
What is happening:
When attempting to move into non-border tiles movement works perfectly
Sometimes the character will not be able to move to a non-border tile if the character is next to a border tile.
What I think may be wrong:
In my code I have Physics2D.Raycast (transform.position, destination, 1f, collisionLayers);
due to my very limited knowledge on how this Raycast works I can assume that I'm using the 3rd argument for distance incorrectly.
Each tile is one world unit so I assumed that the distance I would like to measure is 1f.
I've been working all day on this (and a few other days on a similar project with the same issue) with no luck to finding something similar online. This is my first time posting only for code questions so I hope I haven't left out any important details.
@Vomdrache The problem is probably this: destination
should be the direction not the goal.
The direction would be only Vector3.right
not pos + Vector3.right
! Since you actually use the destination to set the new goal you should pass another Vector3 direction as a parameter.
It would look something like this:
CheckCollision(pos + Vector3.left, Vector3.left);
void CheckCollision(Vector3 destination, Vector3 direction){
Debug.Log (transform.position);
Debug.Log (destination);
RaycastHit2D hit = Physics2D.Raycast (transform.position, direction, 1f, collisionLayers);
if (hit.collider == null) {
pos = destination;
} else {
Debug.Log (hit.collider.gameObject + ": (" + hit.collider.transform.position.x + ", " + hit.collider.transform.position.y + ")");
}
}