Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Supremfire · Aug 25, 2010 at 08:11 AM · raycastbeginner

raycast help need

I want to create a small ray which can tell my villager if there is tree in front of him.

This is what i done but it don't work how i want

function Update  () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.one, hit, 100.0)) {
distanceToGround = hit.distance;
print ("There is something in front of the object!" + distanceToGround);
}
}

Can you help me?

Cheers!

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

2 Replies

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

Answer by skovacs1 · Aug 25, 2010 at 02:28 PM

What your code does:

//Every frame
function Update  () {
    //Casts a ray 100 units in the direction x=-1, y=-1, z=-1 from the current position.
    //You are always casting in the same direction in world space, not relative to your
    //character.
    var hit : RaycastHit;
    if (Physics.Raycast (transform.position, -Vector3.one, hit, 100.0)) {
        //Unless your ground is always diagonally down in world space, this variable is
        //misnamed, especially since the diagonal distance is less consistent and
        //probably not what you wanted anyways.
        distanceToGround = hit.distance;
        //Since you aren't casting in front, this message is wrong.
        print ("There is something in front of the object!" + distanceToGround);
    }
}

What you probably want:

//Every frame
function Update  () {
    //Cast a ray 100 units in the forward direction from the current position
    var hit : RaycastHit;
    if (Physics.Raycast (transform.position, transform.forward, hit, 100.0))
        print ("There is something in front of the object!" + hit.distance);
}

You should really try to understand what your code is doing - it becomes very obvious what's wrong when you know exactly what's happening step by step.

In order for the raycast to work, the object you want to collide with will need to have a collider attached. Also, I recommend using layers and a layermask to reduce the cost of the raycast.

Comment
Add comment · 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
0

Answer by Supremfire · Aug 26, 2010 at 07:27 AM

Ok, thank you very much, it is very helpful.

Comment
Add comment · 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

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

Cannot pick up an object that is the child of another robject? 0 Answers

How can I make my player unable to jump up walls? 2 Answers

Make UI element or button where a physics raycast can´t pass? Is that possible? 1 Answer

Using raycasters to freeze enemies 1 Answer

Help with Raycast on Render Texture 0 Answers


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