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 Marvin Hawkins · Jun 04, 2012 at 04:07 PM · raycastenemymaterials

How to Create enemy flash effect on Raycast hit

Hi,

I have a question. I'm trying to set up an affect where an enemy flashes when hit. Think classic arcade brawlers like X Men, or NES titles like Castlevania:

My problem. I know how to code it when one object collides with another. I'm having trouble doing the same thing with a raycast hit, Which is what my current game is using. So my question, how do I create this effect when using a Raycast vs OnTrigger enter.

Here's my script. It is attached to the object taking a hit, in this case the enemy: (Raycast happens in a different script)

 var materials = new Material[2];
 
 function OnTriggerEnter (  otherobject : Collider)
 {
     if (otherobject.tag == "Player")
     {
         renderer.material = materials[1];
         yield WaitForSeconds(.025);
         renderer.material = materials[0];
     }
 }

Thanks in advance

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
0

Answer by Berenger · Jun 04, 2012 at 04:15 PM

On the player's script, when the ray hit something, check if the gameobject has the enemy script. If it does, call the following function from it :

 function Flash()
 {
     renderer.material = materials[1];
     yield WaitForSeconds(.025);
     renderer.material = materials[0];
 }
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 GutoThomas · Jun 04, 2012 at 04:18 PM

You can add a function in the script which would be hit, and when you get the raycast colliding with a object with this script you just call the function.

 public function flashEffect () {
 
 renderer.material = materials[1];
 yield WaitForSeconds(.025);
 renderer.material = materials[0];
 
 }

I'll assume that the flashEffect function is assigned to a script who's called flashScript, just to exemplify.

Raycast launch:

 var hit: RaycastHit;
 var ray: Ray = GameObject.FindGameObjectWithTag("MainCamera").camera.ScreenPointToRay(Vector3(Screen.width/2, Screen.height/2, 0); // I'm using the center of the screen to detect the collision, as you can see
 
 if(Physics.Raycast(ray, hit)) {
 
 var fs: flashScript = hit.transform.GetComponent(flashScript);
 
 if(fs != null) {
 
 fs.flashEffect();
 
 }
 
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

change object material on mouse click. 2 Answers

What is the Best way to use Raycast for enemy Fire? 2 Answers

Cannot change Materials of certain objects 1 Answer

How to make enemy check if it doesn`t see player for certain times it changes state 1 Answer

Raycasting help, enemy damage 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