Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MC HALO · May 01, 2011 at 06:23 AM · raycastdistanceshootray

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");

//} //} //}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Joshua · May 01, 2011 at 06:31 AM

var distance = hit.distance;

please try to use the docs before posting a question. ;)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image · May 01, 2011 at 06:46 AM 1
Share

Upvoted, but for future reference, linking directly to the relevant doc is common practice/courtesy: http://unity3d.com/support/documentation/ScriptReference/RaycastHit-distance.html

avatar image MC HALO · May 01, 2011 at 06:55 AM 0
Share

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 ?

avatar image Joshua · May 01, 2011 at 07:00 AM 0
Share

@$$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 ;)

avatar image MC HALO · May 01, 2011 at 07:10 AM 0
Share

thanks :) really helped

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to determine multiple points along a ray? 1 Answer

Raycast refuses to cast down 1 Answer

Raycast hitting below mouse position :( 0 Answers

Math - calculate position in world space from ray on infinite plane 2 Answers

Bounds IntersectRay distance isnt correct 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges