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 SrBilyon · Oct 31, 2010 at 09:13 AM · collisionparticlesraycastinggroundemit

Raycasting to check what object (with a particular tag) is underneath the Player

(Player Object is Using the CharacterController component)

This seems like the best way (unless you can do it vectors). I want it to where depending on what type of surface you're on, particles emit to indicate it. (i.e: dust if your on a dirt terrain, grass if on a grass terrain) and to play a sound for the corresponding textures while there verticalDirection > 0. I figured since I can't use triggers or colliders for this, I should use raycasting.

However, I'm a complete novice to raycasting and don't know how I could execute this. I was also wondering if it would be possible to check the texture of the object I'm standing on in case on object that has multiple textures.


Edit: I made a script based on lhk's answer, but I'm getting a error: Unknown Identifier: "origin".

Here is the code:

var dirtPuff : Transform; var timeOut = 1.5; var hitinfo : RaycastHit;

 function Update()
 {
     //RaycastHit hitinfo;

     if(Physics.Raycast(origin,Vector3.down,hitinfo,2))
     {
         if (hitinfo.transform.tag == "Dirt") 
         {  
            Debug.Log("Hit Dirt");
            var clone;
            clone = Instantiate(dirtPuff, hitinfo.transform.position, hitinfo.transform.rotation);
            Destroy(clone.gameObject, timeOut);
            //Instantiate (water_solider_impact);
         }
     }
 }

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

3 Replies

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

Answer by lhk · Oct 31, 2010 at 09:42 AM

The reference says:

static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool 

As you can see you call Physics.Raycast() with the origin and the direction as parameters. A RaycastHit object, the distance/length of the ray and a layer are optional parameters. Physics.Raycast() returns true, if something was hit. To sum up you need to

  • Get the origin of the Ray. For example, the bottom of your player object
  • Get the direction of the Ray. Vector3.down should fit your needs.
  • Create a RaycastHit object as additional parameter
  • Choose the length of the Ray

    RaycastHit hitinfo;
    

    if(Physics.Raycast(origin,Vector3.down,hitinfo,2))

    {

    //something was hit. Now you can get information about the object that was hit by

    //reading the values of hitinfo

    }

    RaycastHit contains

    point

    The impact point in world space where the ray hit the collider.

    normal

    The normal of the surface the ray hit.

    barycentricCoordinate

    The barycentric coordinate of the triangle we hit.

    distance

    The distance from the ray's origin to the impact point.

    triangleIndex

    The index of the triangle that was hit.

    textureCoord

    The uv texture coordinate at the impact point.

    textureCoord2

    The secondary uv texture coordinate at the impact point.

    lightmapCoord

    The uv lightmap coordinate at the impact point.

    collider

    The Collider that was hit.

    rigidbody

    The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.

    transform

    The Transform of the rigidbody or collider that was hit.

    By using

    hitinfo.collider.gameObject
    

    You may access the gameObject the ray has hit. Then you can check wether it is tagged "surface" or something like that. Moreover the hitinfo offers you the desired Texture coordinates.

    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
    1

    Answer by lhk · Nov 01, 2010 at 07:49 AM

    There is no variable named origin in your script. Nevertheless you try to access it in the call ti Physics.Raycast. You could add another line to the Update function, before Physics.Raycast

    var origin : Vector3 = transform.position-Vector3.up;
    

    Note that I'm substracting another Vector of your position. This is meant to avoid intersection from the Ray with the player itself. I don't know wether this is necessary.

    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 Scott Gray · May 28, 2013 at 07:37 PM

    Thank you, Thank you and once more I thank you. This is what I have been searching for for days.

    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

    1 Person is following this question.

    avatar image

    Related Questions

    Emit particles along a animated mesh 1 Answer

    Collision detection accuracy issues. 1 Answer

    Shooting Projectiles Script supposed to delete Projectile, not the parented Particle System 3 Answers

    Obstacle avoidance 1 Answer

    can we attach a collider for each particle emitted by a emitter 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