- Home /
Question by
omegacraft · Oct 16, 2015 at 10:33 PM ·
c#collisionraycastraycastingparkour
How to check if a raycast is not hitting any tagged colliders?
I've been working on a wallrun script and when I do it from the beginning or from the middle its fine, until I use it from the very end, I just float off to the distance. Here is the script
using UnityEngine;
using System.Collections;
public class Wallrun : MonoBehaviour {
public Rigidbody rb;
public Animation anim;
public float thrust;
public float range;
private RaycastHit hit;
private RaycastHit grund;
public bool wruningl = false;
public bool wruning2 = false;
public bool isGrounded = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
if (wruningl) {
StopCoroutine (runLNG());
rb.AddForce (-transform.right * 2000);
anim.Play ("RightRunDone");
wruningl = false;
rb.useGravity = true;
}
if (wruning2) {
StopCoroutine (runLNG());
rb.AddForce (transform.right * 2000);
anim.Play ("LeftRunDone");
wruning2 = false;
rb.useGravity = true;
}
}
if (Physics.Raycast (transform.position, transform.right, out hit, range)) {
if (hit.collider.gameObject.tag == ("RunWall")) {
if (Input.GetKeyDown (KeyCode.E) && !isGrounded && Input.GetKey (KeyCode.W)) {
wruningl = true;
anim.Play ("RightRun");
StartCoroutine (runLNG ());
rb.useGravity = false;
rb.AddForce (Vector3.down * thrust);
}
}
} /*else {
StopAllCoroutines ();
rb.useGravity = true;
}*/
if (Physics.Raycast (transform.position, -transform.right, out hit, range)) {
if (hit.collider.gameObject.tag == ("RunWall")) {
if (Input.GetKeyDown (KeyCode.E) && !isGrounded && Input.GetKey (KeyCode.W)) {
wruning2 = true;
anim.Play ("LeftRun");
StartCoroutine(runLNG());
rb.useGravity = false;
rb.AddForce (Vector3.down * thrust);
}
}
}/*else {
StopAllCoroutines ();
rb.useGravity = true;
}*/
}
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Grund") {
isGrounded = true;
Debug.Log ("LOL");
}
}
void OnCollisionExit(Collision othere) {
if (othere.gameObject.tag == "Grund") {
isGrounded = false;
Debug.Log ("LOL1");
}
}
IEnumerator runLNG () {
yield return new WaitForSeconds (1.4f);
rb.useGravity = true;
if (wruningl) {
anim.Play ("RightRunDone");
}
if (wruning2) {
anim.Play ("LeftRunDone");
}
wruningl = false;
wruning2 = false;
}
}
I commented out the "else" function because when I start wallruning the gravity should be disabled but it is enabled if I don't comment it out
Comment
Your answer
Follow this Question
Related Questions
What's a reliable way to detect if an object is no longer being hit by a ray? 1 Answer
Obstacle avoidance 1 Answer
Help Understanding Raycast 2 Answers
Multiple Cars not working 1 Answer
Raycast doesn't collide as it should 1 Answer