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 IIStalkerII · Aug 16, 2013 at 08:33 AM · physicsraycastfunctionslowsendmessage

Raycast is being unreliable

Hi there,

I have a script that i use to Raycast to an object then send a message to it to execute a function. It works, the problem being that it is very unreliable and seems to act on a kind of delay.

For example, i will approach my enemy, looking directly at him and it is only when I go around to the back of his model and stare at it that the Raycast hits him, sends the message and then executes the desired function. Here is both the Raycast script and the script attached to the enemy.

  var hit : RaycastHit;
     var TheDammage : int = 100;
  
      
     function Update()
     {
     if ( Physics.Raycast( transform.position, transform.forward, hit, 3 ))
     {
     Debug.Log( "ray hit (tag) : " + hit.collider.gameObject.tag + " : " );
      
     if ( hit.collider.gameObject.tag == "EnemyStatic" )
     {
     Debug.Log("HitStaticEnemy");
     hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
     }
     }
     }



pragma strict

@script RequireComponent(AudioSource)

var Soundplayed1 = false; var HorrorHit1: AudioSource; var Health = 100;

 function ApplyDammage (TheDammage : int)
 {
     Health -= TheDammage;
     
     if(Health <= 0)
     if(Soundplayed1 == false)
     {
         Dead();
     }
 }
 
 function Dead()
 {
     GetComponentInChildren(MeshRenderer).enabled = false;
     Debug.Log("MeshHit");
     audio.volume = 1.0;
     HorrorHit1.Play();
     Soundplayed1 = true;
     Destroy (gameObject, 5);
 }
Comment
Add comment · Show 9
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 IIStalkerII · Aug 16, 2013 at 08:06 AM 0
Share

Also I am aware of the delay in destroying the gameobject. This is intentional as I wanted to allow the audio clip to finish before removing the audiosource.

avatar image 2d4Games · Aug 16, 2013 at 11:36 AM 0
Share

Are you sure its acting on a delay? From your description, it may only be hitting the character from the back.

Also, as just a quick test, try moving your raycast code to FixedUpdate ins$$anonymous$$d of just Update. FixedUpdate is called more often.

avatar image IIStalkerII · Aug 16, 2013 at 12:11 PM 0
Share

O$$anonymous$$ i changed from Update () to FixedUpdate () no change really.

I'm getting debug log readouts that the ray is hitting untagged colliders (terrain) then after staring at the back of my enemy model for about 5 seconds, the desired function is executed. I am really stumped with this one. But I do think it is a problem within the first script. because as soon as the raycast hits the enemy, it calls the desired function through Sendmessage.

avatar image smrt_co · Aug 16, 2013 at 01:53 PM 0
Share

Try using the Debug.DrawRay() function to see what is your ray hitting exactly. The line will be drawn in scene view.

On which game object is the first script attached? On FP camera, player mesh?

avatar image Xtro · Aug 16, 2013 at 02:12 PM 1
Share

Correction on 2d4Games: FixedUpdate is called more often ONLY if the frame rate is lower than the fixed delta time. Not always.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Aug 17, 2013 at 03:53 AM

Try this:

 // [...]
 
 function GetHierarchyName(obj : Transform) : String
 {
     if (obj == null)
         return "ROOT";
     return GetHierarchyName(obj.parent) + "/" + obj.name;
 }
 
 function Update()
 {
     if ( Physics.Raycast( transform.position, transform.forward, hit, 50 ))
     {
         Debug.Log("We hit: " + GetHierarchyName(hit.collider.transform));
         Debug.DrawLine(transform.position, hit.point, Color.yellow, 0.5);
     }
 }


this will print the full path in the hierarchy of the object your ray hit. There will also be a line from your ray starting point to the actual hit point. This line will last half a second just to see where the ray actually hits.

ps: you should undock either the scene or game view or dock them next to each other so you can see both at the same time. It's important that you move the scene view camera to a spot where you have a good view of what happens.

There's not much we can do about your problems since we can be sure that the raycast works perfectly and without any delay.

pps: Is it possible that the variable "TheDammage" has a different value in the inspector? The inspector holds the actual value so it doesn't need to be 100 as you might expect.

Comment
Add comment · Show 1 · 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 IIStalkerII · Aug 19, 2013 at 01:08 PM 0
Share

Thank you so much!

Using your debug script I managed to find out that my raycast was hitting a plane in front of the camera which i use for a camera effect, which is weird because i had disabled the collider on it.

Now by adding the plane to the ignore raycast layer. It all works perfectly!

Thank you very much all of you for your help and especially Bunny83.

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Physics.CapsuleCast Help 1 Answer

Raycast stops working after ~40 seconds when target has Rigidbody 2 Answers

Unable to set collision layer from scrip 0 Answers

Realistic Helicopter AI Movements using physics 0 Answers

Make a rolling ball always on ground without falling when reaching the edges of the map 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