- Home /
Raycast Help
This is sample of my Pac-Man movement code with a shell for the raycast. Every time I try to make some type of raycast restriction he ends up being stuck at the end of the first lane.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody2D rb2D;
public float speed = 0.3f;
private Vector2 velocity;
private Animator anim;
private Vector2 newPos;
void Start()
{
newPos = transform.position;
rb2D = GetComponent<Rigidbody2D>();
anim = GetComponent <Animator> ();
velocity = Vector2.left;
}
void FixedUpdate()
{
rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
if (Input.GetAxisRaw ("Vertical") == (1)) {
if(valid(Vector2.up))
{
velocity = Vector2.up;
anim.SetFloat ("dirY", 1);
anim.SetFloat ("dirX", 0);
rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
}
}
if (Input.GetAxisRaw ("Horizontal") == (-1)) {
if(valid(Vector2.left))
{
velocity = Vector2.left;
anim.SetFloat ("dirX", -1);
anim.SetFloat ("dirY", 0);
rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
}
}
if (Input.GetAxisRaw ("Vertical") == (-1)) {
if(valid(Vector2.down))
{
velocity = Vector2.down;
anim.SetFloat ("dirY", -1);
anim.SetFloat ("dirX", 0);
rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
}
}
if (Input.GetAxisRaw ("Horizontal") == (1)) {
if(valid(Vector2.right))
{
velocity = Vector2.right;
anim.SetFloat ("dirX", 1);
anim.SetFloat ("dirY", 0);
rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
}
}
}
bool valid (Vector2 velocity)
{
}
}
This is a photo of where he gets stuck. My colliders are lined up perfectly, and without the raycast he can move around the map perfectly.
I would appreciate some pseudo code, but not the actual code to make it work.
I put a line in the script to log the tags the raycast gets to the console. When Pac-$$anonymous$$an gets to the spot in the picture above he can not turn up or down. The tag detected is a wall, which explains why he can't move, but there is no wall in his way, just pac-dots. I also observed something else weird. At times the console logs the tag its hitting as player, but since Pac-$$anonymous$$an is less than 1 unit and so is the collider why does it ever hit him.
Answer by LaneFox · Jul 06, 2015 at 11:40 AM
Are you using Physics*2D*.Raycast() ?
Small side note.. GetAxisRaw should return 0 or 1 but in dealing with floats its always good practice to just go ahead and assume you'll never get an equal number due to floating point precision. I would set this up with <= / >=
operators, but I'm OCD. :P
Physics2D.Raycast(stuff)
if (hit) return false;
else return true;
GetAxisRaw returns a value of -1 or 1 exactly because it only returns whole numbers. I've tried the script above checking whether the ray collides with a collider tagged "wall" but that caused him to become stuck as mentioned before.
Right, and that should work fine. Can you post your raycast code?
I'll try to type this using my phone. I'm not at my PC right now but this code is similar to what i was trying to use. bool valid (Vector2 velocity) { RaycastHit2D hit = Physics2D.Raycast (newPos, velocity, 1); return (hit.collider.gameObject.tag != "wall"); }
It could be getting hung up on cases where there is no hit collider, add a check to narrow it down.
private bool valid(Vector2 inputVector)
{
RaycastHit2D hit = Physics2D.Raycast(newPos, inputVector, 1);
if (hit.collider != null)
{
return (hit.collider.gameObject.tag != "wall");
}
return true;
}
After that if it still fails, are you certain that all of the walls are tagged "wall" and that there is adequate space for the character to fit between the colliders?
Sadly the script above did not work either. Pac-$$anonymous$$an still ends up stuck at the end of the first lane. As mentioned before I know that Pac-$$anonymous$$an can fit through all lanes because I can collect all the Pac-Dots without the raycast script. I have not mentioned something that might be an issue and thats that the walls are part of one sprite with multiple Box colliders, but im positive they are tagged "wall".