- Home /
Raycast isn't comparing tags
I'm making a script and when you fall, if you are within a certain distance from the ground you can roll and get out of getting damaged. I'm using a raycast to find out how close I am.
But none of my debugs show up while testing! What is wrong with the code? (The tag is the same on the floor. I didn't forget to capitalize or anything.)
var normal : float = 6.0; var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0; var sprint : float = 10.0; var pause = false; var health : int = 100; var falling : float = 0.00;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var ray = transform.TransformDirection (-Vector3.up); var controller : CharacterController = GetComponent(CharacterController); var hit : RaycastHit;
if (controller.isGrounded) { // We are grounded, so recalculate // move direction directly from axes moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;
// Sprint by pressing Left Shift Sprint();
// Look around by moving the mouse LookAround();
// Jump if (Input.GetButton("Jump")) { moveDirection.y = jumpSpeed; } }
// Placeholder for pause screen. Pause();
// The fall damage stuff if (Physics.Raycast (transform.position , -Vector3.up, hit, 10)){ if (hit.collider.gameObject.CompareTag ("Floor")) { print ("The Floor is under you"); if (!controller.isGrounded){ Debug.Log ("You are in the air!"); if (Input.GetAxis ("Jump")){ Debug.Log ("You rolled!"); moveDirection.y = 10000; } } } }
//End fall damage stuff
// Apply gravity moveDirection.y -= gravity * Time.deltaTime;
// Move the controller controller.Move(moveDirection * Time.deltaTime); }
// the functions that we called higher up function Sprint() {
if (Input.GetButton ("Sprint")) {
speed = sprint;
};
else {
speed = normal;
}
};
function LookAround() {
var x = Input.GetAxis ("Mouse X"); transform.Rotate (0, x, 0);
}
function Pause() { if (Input.GetButtonDown("Pause")) { pause = !pause; Debug.Log ("Paused,");
if (pause == true){
Screen.lockCursor = false;
}
} else { Screen.lockCursor = true; } }
function Roll() {
}
When I have a debug right after the raycast itself I receive the debug, but only the one after the raycast. Any help would be appreciated.
EDIT: Decided to clarify on what is the problem, I'm not getting any errors, the raycast isn't realizing that the floor is tagged "Floor". I'm guessing I'm not doing this right, but I'm not sure.
Answer by Joshua · May 10, 2011 at 11:44 PM
I just looked up CompareTag() , apperently it really is a function. I've never seen it used. What I have always done and have seen in all other examples on this site is simply use gameObject.tag == string.
So in this case:
if (hit.collider.gameObject.tag == "Floor") { ...
see if that fixes it. Also, make sure the capitalization is right, it's I probably the most common error I see on this site xD
Good luck with it!
There are capitalized right, that didn't fix it, however a friend of $$anonymous$$e pointed out that unless I wanted to only be able to roll of the ground and not any props then I shouldn't use tags and only use colliders. Which fixed it. Thanks though!