• 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
1
Question by Chris 35 · Apr 21, 2011 at 02:48 AM · javascriptraycastraycastingdetection

ray cast not working ?

im trying to cast a ray from my player that will detect object it touches a certain length in front of him. i swear my code is fine but i cant get it to work

var xDirection = 15; var yDirection = 0; var zDirection = 45; var length = 0.1; var hit : RaycastHit;

function Update () { var diagonal = transform.TransformDirection (Vector3(xDirection,yDirection,zDirection)); Debug.DrawRay(transform.position, diagonal * length,Color.green);

if (Physics.Raycast(transform.position, diagonal,length)) { Debug.Log ("There is something in front of the object!"); }

}

help would be great :)

EDIT ANOTHER PROBLEM.....

var Bullet1_direction = Vector3(15,0,45); var Bullet2_direction = Vector3(0 ,0,1); var Bullet3_direction = Vector3(-9 ,0,45); var length = 4;

function Update () { var Bullet1_diagonal = transform.TransformDirection(Bullet1_direction); Bullet1_diagonal.Normalize();

var Bullet2_diagonal = transform.TransformDirection(Bullet2_direction); Bullet2_diagonal.Normalize();

var Bullet3_diagonal = transform.TransformDirection(Bullet3_direction); Bullet3_diagonal.Normalize();

Debug.DrawRay(transform.position, Bullet1_diagonal length,Color.green); Debug.DrawRay(transform.position, Bullet2_diagonal length,Color.red); Debug.DrawRay(transform.position, Bullet3_diagonal * length,Color.blue);

if (Physics.Raycast(transform.position, Bullet1_diagonal,length)) { Debug.Log ("Bullet 1 hit green!"); } if (Physics.Raycast(transform.position, Bullet2_diagonal,length)) { Debug.Log ("Bullet 2 hit red!"); } if (Physics.Raycast(transform.position, Bullet3_diagonal,length)) { Debug.Log ("Bullet 3 hit blue!"); }

}

HELP PLEASE... @Bunny83

Comment
Add comment · Show 3
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 Marowi · Apr 21, 2011 at 03:48 AM 0
Share

Can you see the Debug.DrawRay in the Scene window? Is 0.1 long enough for the raycast length? Does the object that you expect it to hit have a Collider?

avatar image MC HALO · Apr 21, 2011 at 04:04 AM 0
Share

You can see his debug.DrawRay and 0.1 is fine i have tested it and it works for me :)

avatar image Chris 35 · Apr 21, 2011 at 04:34 AM 0
Share

yeah 0.1 is fine and ive tried 1,2,3 ect but still nothing, i only get a result if its like around 10000

2 Replies

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

Answer by Bunny83 · Apr 21, 2011 at 09:58 AM

I would say your problem is your direction. If you use Raycast with a length the direction is normalized! That means your ray is 0.1 units long. But if you draw your ray with diagonal * length the drawn ray will be 4.743 units long since your direction is 47,43... long. I don't even understand your direction. It's forward but a bit to the right? Try diagonal.Normalize(); or .normalized.

var direction = Vector3(15,0,45); var length = 0.1;

function Update () { var diagonal = transform.TransformDirection(direction); diagonal.Normalize(); Debug.DrawRay(transform.position, diagonal * length,Color.green);

 if (Physics.Raycast(transform.position, diagonal,length)) {
     Debug.Log ("There is something in front of the object!");
 }

}

Comment
Add comment · Show 3 · 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 Chris 35 · Apr 21, 2011 at 11:06 AM 0
Share

THAN$$anonymous$$ YOU SO $$anonymous$$UCH :D it worked :) i dont think i would ever of figured that out :P Thanks a million

avatar image Chris 35 · Apr 22, 2011 at 05:35 AM 0
Share

could you please have a look at this bit of code, i cant get it to work im having the same problem with the numbers having to be decimal to get right distance rather then full numbers

avatar image Bunny83 · Apr 23, 2011 at 02:33 AM 0
Share

Sorry, i'm not at home ;) but i don't get what's your problem? If your problem is that you want to declare a float variable ins$$anonymous$$d of an int, just add ".0" to your number. That will turn the variable into a float. var length = 4.0; You can even declare the type explicitly with: var length : float = 4.0;

avatar image
0

Answer by MC HALO · Apr 21, 2011 at 03:18 AM

i think you forgot the hit :

var xDirection = 15; var yDirection = 0; var zDirection = 45; var length = 0.1; var hit : RaycastHit;

function Update () { var diagonal = transform.TransformDirection (Vector3(xDirection,yDirection,zDirection)); Debug.DrawRay(transform.position, diagonal * length,Color.green);

if (Physics.Raycast(transform.position, diagonal,hit,length)) { Debug.Log ("There is something in front of the object!"); } }

hope it works

have you attached it to the right player? if you still can not get it to work send it to me to my e-mail and i can look at it for you. the project folder this is.

My e-mail:

Hotmail- gto_oni-eyes@hotmail.com

google:

Hummad.Nazir@gmail.com

i hope to hear from you :)

try my code:

public var RayCastHitDoor :float = 5.0f;

// Update is called once per frame

function Update () {

var hit : RaycastHit ;

 Debug.DrawRay(transform.position, Vector3.forward * RayCastHitDoor, Color.red);

     if(Physics.Raycast(transform.position , Vector3.forward, hit, RayCastHitDoor))

 {

     Debug.DrawRay(transform.position,Vector3.forward * RayCastHitDoor , Color.green);

            if(hit.collider.gameObject.tag == "put your obj name in here "){


                // then do something. in my case i have told the hit collider to play animation when it collides with the object that is tagged door

     hit.collider.gameObject.animation.Play("Door animation");   



     }



 }



}

instead of having animation you could have Destroy the object if you want to do that i would be like this:

     Destroy(hit..collider.gameObject);

and that should destroy the game object you can do what ever you like :)

Comment
Add comment · Show 8 · 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 Marowi · Apr 21, 2011 at 03:33 AM 1
Share

The definition he's used works just as well. Check http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

avatar image MC HALO · Apr 21, 2011 at 03:39 AM 0
Share

yep true that works for me as well

avatar image Chris 35 · Apr 21, 2011 at 04:35 AM 1
Share

ive tried with the hit and with out it still doesn't work :\

avatar image MC HALO · Apr 21, 2011 at 05:40 AM 0
Share

send it to me i will have a look

avatar image MC HALO · Apr 21, 2011 at 05:41 AM 0
Share

here is my e-mail address :

gto_oni-eyes@hotmail.com

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Raycast help..... 2 Answers

Turret that shots a raycast to detect my character then shots at him. 1 Answer

Raycasting problems 1 Answer

Setting Scroll View Width GUILayout 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges