- Home /
hopelessly long, no image, will never be answered
Raycast detects walls, how to disable movement or input in direction of the wall?
Hi All,
I have a simple 2d movement script and a simple raycast script that will detect when the player touches a wall. I have no problem detecting the walls but I am not sure how to disable the player movement in the direction of the wall so that they can not walk through it.
This is a 2d setup similar to old jrpgs, like Final Fantasy for the NES, if that helps to envision the mechanics. Of course, I will need to be able to enable movement toward the wall again once the ray no longer detects the wall, i.e. after the player moves far enough in the opposite direction, but maybe I can figure that part out if someone can help me with disabling.
Movement script:
var speed: float = 15;
// Link to the animated sprite
private
var anim: tk2dSpriteAnimator;
// This script must be attached to the sprite to work.
anim = GetComponent(tk2dSpriteAnimator);
function FixedUpdate() {
if (Input.GetButton("Horizontal")) {
rigidbody.velocity = Vector3(speed, 0, 0);
if (!anim.IsPlaying("right")) {
anim.Play("right");
}
return;
}
if (Input.GetButton("-Horizontal")) {
rigidbody.velocity = Vector3(-speed, 0, 0);
if (!anim.IsPlaying("left")) {
anim.Play("left");
}
return;
}
if (Input.GetButton("Vertical")) {
rigidbody.velocity = Vector3(0, speed, 0);
if (!anim.IsPlaying("up")) {
anim.Play("up");
}
return;
}
if (Input.GetButton("-Vertical") && Time.time > 1) {
rigidbody.velocity = Vector3(0, -speed, 0);
if (!anim.IsPlaying("down")) {
anim.Play("down");
}
return;
}
if (Input.GetButtonUp("-Vertical")) {
if (!anim.IsPlaying("idle_down")) {
anim.Play("idle_down");
}
}
if (Input.GetButtonUp("Vertical")) {
if (!anim.IsPlaying("idle_up")) {
anim.Play("idle_up");
}
}
if (Input.GetButtonUp("-Horizontal")) {
if (!anim.IsPlaying("idle_left")) {
anim.Play("idle_left");
}
}
if (Input.GetButtonUp("Horizontal")) {
if (!anim.IsPlaying("idle_right")) {
anim.Play("idle_right");
}
}
}
Raycast script:
function FixedUpdate () {
var up = transform.TransformDirection(Vector3.up);
var right = transform.TransformDirection(Vector3.right);
var hit : RaycastHit;
Debug.DrawRay(transform.position, -up * 0.3, Color.green);
Debug.DrawRay(transform.position, up * 0.9, Color.green);
Debug.DrawRay(transform.position, -right * 0.9, Color.green);
Debug.DrawRay(transform.position, right * 0.9, Color.green);
if(Physics.Raycast(transform.position, -up, hit, 0.3)){
Debug.Log("Hit");
if(hit.collider.gameObject.name == "BottomWall"){
//Disable movement or input here
}
}
if(Physics.Raycast(transform.position, up, hit, 0.9)){
Debug.Log("Hit");
if(hit.collider.gameObject.name == "TopWall"){
//Disable movement or input here
}
}
if(Physics.Raycast(transform.position, -right, hit, 0.9)){
Debug.Log("Hit");
if(hit.collider.gameObject.name == "LeftWall"){
//Disable movement or input here
}
}
if(Physics.Raycast(transform.position, right, hit, 0.9)){
Debug.Log("Hit");
if(hit.collider.gameObject.name == "RightWall"){
//Disable movement or input here
}
}
}
Thanks!
(1) nobody will, ever, read that much code. (2) just include a SCREEN SHOT so people know what is going on.
To help, I've delete this question - ask a new one or you'll never get an answer. Cheers
Couldn't you just use the Collision system built within Unity?