- Home /
Need help with raycast movement.
Hi. So I am making a maze game for practice. Using a maze generator script. I am moving my character with arrow keys using simple movement script.On android it moves with a swipe(left,right,up,down).Now what I want is that once a finger is swiped, the player(character in game) should move continuously making all possible turns till it reaches a point where it needs instruction from user to turn(suppose it reaches a point where there are 2 turns left and right).But if there is dead end ahead and space on right side,it should automatically turn right(90degree). I want it to be like a android game 'Maze King'. I got to know raycast would be nice and easy way to do it..I have never used raycast. Any help regarding same would be much appreciated. Below is script....character is moving and rotating left and up(with arrow keys) just fine. HOwever not all perfect
using UnityEngine; using System.Collections;
public class Move : MonoBehaviour {
// Use this for initialization
public float speed = 0.3f;
Vector2 dist = Vector2.zero;
public Quaternion lookLeft = Quaternion.Euler(0, 0, 0);
public Quaternion lookRight = Quaternion.Euler(0, 0, 0);
public Quaternion lookTop = Quaternion.Euler(0, 0, 0);
public Quaternion lookDown = Quaternion.Euler(0, 0, 0);
RaycastHit2D raycastgo;
RaycastHit2D raycastleft;
RaycastHit2D raycastright;
RaycastHit2D raycastdown;
public bool movementbool = false;
public bool movementbool2 = false;
public bool movementbool3 = false;
public bool movementbool4 = false;
public GameObject Player;
public Transform player;
void Start () {
dist = transform.position;
raycastgo = Physics2D.Raycast(transform.position, Vector2.up, 10, 1 << LayerMask.NameToLayer("MazeBox"));
raycastleft = Physics2D.Raycast(transform.position, -Vector2.right, 10, 1 << LayerMask.NameToLayer("MazeBox"));
raycastright = Physics2D.Raycast(transform.position, Vector2.right, 10, 1 << LayerMask.NameToLayer("MazeBox"));
raycastdown = Physics2D.Raycast(transform.position, -Vector2.up, 10, 1 << LayerMask.NameToLayer("MazeBox"));
}
// Update is called once per frame
void Update(){
}
void FixedUpdate () {
Vector2 p = Vector2.MoveTowards(transform.position, dist, speed);
rigidbody2D.MovePosition(p);
if ((Vector2)transform.position == dist) {
if(!raycastright || !raycastgo || !raycastleft || !raycastdown || !raycastright && !raycastgo || !raycastright && !raycastleft && !raycastgo || !raycastleft && !raycastgo && !raycastdown || !raycastright && !raycastleft && !raycastdown || !raycastright && !raycastleft || !raycastgo && !raycastdown || !raycastleft && !raycastgo || !raycastdown && !raycastleft || !raycastright && !raycastgo || !raycastright && !raycastdown){
Takeinput();
}
if(movementbool==true && !raycastgo && valid(Vector2.up)){
dist = (Vector2)transform.position + Vector2.up;
transform.rotation = lookTop;
movementbool = false;
}
if(movementbool3==true && !raycastdown && valid(-Vector2.up)){
dist = (Vector2)transform.position - Vector2.up;
transform.rotation = lookDown;
movementbool3 = false;
}
if(movementbool4==true && !raycastleft && valid(-Vector2.right)){
dist = (Vector2)transform.position - Vector2.right;
transform.rotation = lookLeft;
movementbool4 = false;
}
if(movementbool2==true && !raycastright && valid(Vector2.right)){
dist = (Vector2)transform.position + Vector2.right;
transform.rotation = lookRight;
movementbool2 = false;
}
}
}
bool valid(Vector2 dir) {
Vector2 pos = transform.position;
RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
return (hit.collider == collider2D);
}
public void Takeinput()
{
if (Input.GetKey(KeyCode.UpArrow)){
movementbool = true;
}
if (Input.GetKey(KeyCode.RightArrow)){
movementbool2 = true;
}
if (Input.GetKey(KeyCode.DownArrow)){
movementbool3 = true;
}
if (Input.GetKey(KeyCode.LeftArrow)){
movementbool4 = true;
}
}
}
Answer by abi-kr01 · Mar 28, 2015 at 05:29 AM
if i am understanding it rit you need to add constant movement to player ones user swap . like in a temple run player will keep on running and your swap give a direction to run . You can add relative velocity to your player it will be constant and depends on you play transform .
thanks for ur reply...yes the character has to move at constant speed.Lets say its in maze and it reaches a point where there is dead end ahead and wall on left and right open.so it should turn to right by itself. User should get the option to swipe for turn only if there are more than 1 turn possible.."maze king" is a android game based on this type of movement.
use four raycast from the player pos the one which don't have hit