- Home /
How can i tell the distance from a raycast?
hi guy, i was wondering if someone can help me. i have created a update function on the top of my script with rays. each line of ray has its own distance set up at the top, but what i want to find out is if the player hits the wall with the tag thats in the ray i want to be able to find out at what distance the ray line hits the wall. if i jump of the wall i want it to tell me how far i have moved from the wall. is this possible at all :) i would be really glad if some one can help me :) Thanks in advance :) MCHALO here is my script
private var xOffset = 0.0; var Water : float = 4; //public var lockedRotation: float = 0; var speed : float = 5; var jump : float = 10; var DoubleJump : int = 1; var JumpReset : int = 2; var JumpResetWallHit : int = 1; var JumpVariance : float = 5; //ar CenMass : float = 1; var HitWallCollider: boolean = false; var RayCastDist : float = 3; var LeftRayDist : float = 3; //var springSpeed : float = 10; var ClimbSpeed : float = 5; static var Climbing: boolean = false;
var WalkingRight : boolean = false; var WalkingLeft: boolean = false; //var SecWait : float = 0.3; function Awake (){
//rigidbody.constraints = RigidbodyConstraints.FreezePositionZ| RigidbodyConstraints.FreezeRotationZ //|RigidbodyConstraints.FreezeRotationY| RigidbodyConstraints.FreezeRotationX;
}
function Update () {
var Hit : RaycastHit; var Hit2 : RaycastHit;
Debug.DrawRay(transform.position , Vector3.right * RayCastDist, Color.green);
//var fwd = transform.TransformDirection (Vector3.right);
if (Physics.Raycast (transform.position, Vector3.right,Hit, RayCastDist))
{
if(Hit.collider.gameObject.tag == "Wall"){
print ("Right side hit");
WalkingLeft = false;
WalkingRight = true;
} } Debug.DrawRay(transform.position , Vector3.left* LeftRayDist, Color.green);
// var fwd = transform.TransformDirection (Vector3.right);
if (Physics.Raycast (transform.position, Vector3.left,Hit2, LeftRayDist))
{
if(Hit2.collider.gameObject.tag == "Wall 2"){
print ("Left Side hit ");
WalkingLeft = true;
WalkingRight = false;
} }
}
function Start () {
//rigidbody.isKinematic = true;
} function FixedUpdate () { //var movement : float = Input.GetAxisRaw("Horizontal") speed; //movement = Time.deltaTime;
//rigidbody.centerOfMass = Vector3 (0, CenMass, 0);
if(WalkingRight == false){
if(Input.GetKey("d"))
{
rigidbody.velocity = Vector3(speed , rigidbody.velocity.y, 0);
}
}
if(WalkingLeft == false){
if(Input.GetKey("a")){
rigidbody.velocity = Vector3(-speed , rigidbody.velocity.y, 0) ;
}
if(WalkingLeft == true || WalkingRight == true){
rigidbody.freezeRotation = true;
}
}
if (DoubleJump > 0)
{
if (Input.GetKeyDown(KeyCode.Space))
{
rigidbody.isKinematic = false;
rigidbody.velocity = Vector3(0,jump ,0);
DoubleJump -= 1;
}
}
if (Input.GetKey(KeyCode.Space))
{
rigidbody.isKinematic=false;
rigidbody.AddForce(Vector2(0,JumpVariance));
Climbing = false;
//constantForce.relativeForce = Vector3(0, -10, 0);
}
if (Climbing == false)
{
rigidbody.isKinematic = false;
//transform.Translate(Vector3(movement,0,0)); // you need to change this line dude it is making the code
// play up. because you have movement defined up above this part would not let my booleans work and the is really making
// the jump play about. So see if you can find another line of code for this :)
}
if (Climbing == true)
{
//transform.localPosition.x = 0;
transform.rotation = transform.parent.rotation;
transform.parent.gameObject.rigidbody.AddRelativeForce(Vector3.right * Input.GetAxis("Horizontal") * 3, ForceMode.VelocityChange);
}
if(Input.GetButtonDown("Jump")&& (HitWallCollider == true )){
Debug.Log("Gravity is true player will fall");
rigidbody.useGravity = true;
Debug.Log("Drag is set back to 0");
rigidbody.drag = 0;
Debug.Log("Kinematic is set to false move free");
rigidbody.isKinematic = false;
Debug.Log("Hey! you hit the wall DoubleJump is reset");
Debug.Log("Not hitting wall");
HitWallCollider = false;
WalkingRight= false;
WalkingLeft= false;
} }
function OnCollisionStay ( other : Collision) { if (other.gameObject.tag == "Ground") { if(!Input.GetAxis("Horizontal")) { rigidbody.velocity.x = 0;
}
}
}
function OnCollisionEnter ( hit : Collision) { if (hit.gameObject.tag == "Ground") { //if(other.transform.position.y<transform.position.y)
DoubleJump = JumpReset;
WalkingLeft = false;
WalkingRight = false;
}
if (hit.gameObject.tag == "Wall" ){
Debug.Log("Hitting the wall"); rigidbody.isKinematic = true; Debug.Log("can not move Kin = ture"); //rigidbody.useGravity = false; Debug.Log("drag is set hold player still "); rigidbody.drag = 13;
Debug.Log("You hit the wall"); DoubleJump = JumpReset; //rigidbody.AddForce(0, 0, 0); HitWallCollider = true; //rigidbody.constraints = RigidbodyConstraints.FreezePositionZ| RigidbodyConstraints.FreezeRotationZ //|RigidbodyConstraints.FreezeRotationY| RigidbodyConstraints.FreezeRotationX; Debug.Log("Waiting 5 sec before you drop"); yield WaitForSeconds(0.3);
Debug.Log("Gravity is true you will fall");
rigidbody.useGravity = true;
Debug.Log(":)");
rigidbody.drag = 0;
}
if (hit.transform.tag == "Ground"){
HitWallCollider = false;
rigidbody.isKinematic = false;
//rigidbody.useGravity = true;
rigidbody.drag = 0;
}
}
function OnTriggerStay ( other : Collider) { if (other.gameObject.tag == "ladder") { if (Input.GetKey("w")) { transform.Translate ( Vector2(0, ClimbSpeed Time.deltaTime)); rigidbody.isKinematic = true; DoubleJump = JumpReset; Climbing = true; } if (Input.GetKey("s")) { transform.Translate (Vector2(0, -ClimbSpeed Time.deltaTime)); rigidbody.isKinematic = true; DoubleJump = JumpReset; Climbing = true; }
if (transform.parent.tag == "ladder")
{
transform.localPosition.x = 0;
//transform.rotation = transform.parent.rotation;
//transform.parent.gameObject.rigidbody.AddRelativeForce(Vector3.right * Input.GetAxis("Horizontal") * 10, ForceMode.VelocityChange);
}
}
if (other.gameObject.tag=="water")
{
DoubleJump=JumpReset;
rigidbody.velocity.y/=Water;
}
}
function OnTriggerEnter ( other : Collider) { if (other.gameObject.tag=="water") { jump*=Water; } }
function OnTriggerExit ( other : Collider) {
if (other.gameObject.tag == "ladder")
{
rigidbody.isKinematic = false;
transform.rotation = Quaternion.identity;
Climbing = false;
transform.parent = null;
}
if (other.gameObject.tag=="water")
{
jump/=Water;
}
} function SetXOffest(offset: float){ xOffset = offset; }
//function WallCast (){ // var hit : RaycastHit;
// Debug.DrawRay (transform.position, Vector3.right * RayCastDist, Color.green);
//if(Physics.Raycast(transform.position, Vector3.right, RayCastDist, 10)){ //Debug.DrawRay (transform.position, Vector3.right * RayCastDist, Color.red); //if(hit.collider.gameObject.tag == "Wall"){ //Debug.Log("Hey You hit the wall congratz");
//} //} //}
Answer by Joshua · May 01, 2011 at 06:31 AM
var distance = hit.distance;
please try to use the docs before posting a question. ;)
Upvoted, but for future reference, linking directly to the relevant doc is common practice/courtesy: http://unity3d.com/support/documentation/ScriptReference/RaycastHit-distance.html
i did this in the print function "print (Hit.distance);
then that gave me my when i hit the wall it was 0.4799995. now what i need is i need it to tell me if it is less then this then do something. is this the correct way :
if(hit.distance < 0.4799995){
}
is that the correct way ?
@$$anonymous$$C HALO, yes, that looks correct. @$$anonymous$$arowi,I guess you're right, I usually do that but here it looks like he just needed a links to the docs in general so he could use them at all ;)