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 RyanThePro · Jun 30, 2012 at 05:17 PM · javascriptinstantiateraycastvariable

Changing variable in a script of a game object hit with raycast

I have a script that is my gun shooting script (with raycast), and another one that has my enemies health. I want to know how to make it so that when my enemy gets hit with raycast, the variable zHealth goes down for only the zombie hit with raycast. The enemies are all instantiated clones of the same game object.

shootingGunScript

 #pragma strict
 var zHealth = 3;
 var spawnPoint : Transform;
 var muzzleFlash : Renderer;
 var MuzzleLight : Light;
 var Zombiebehaviourscript : Zombiebehaviourscript;
 //var shotSound : AudioClip;
 
 
 function Start () {
 muzzleFlash.enabled = false;
 MuzzleLight.enabled = false;
 
 }
 
 function Update () {
 
 if(Input.GetMouseButtonDown(0)){
 
 var hit : RaycastHit;
 var rayray = Physics.Raycast(spawnPoint.position, spawnPoint.forward, Mathf.Infinity);
 Debug.DrawRay (spawnPoint.position, spawnPoint.forward*10, Color.green);
 Shoot();
 
 if (Physics.Raycast(spawnPoint.position, spawnPoint.forward, hit, Mathf.Infinity)){
 Zombiebehaviourscript.zHealth -= 1;
 }
 
   
 
 }
 
 }
 
 function Shoot (){
 muzzleFlash.renderer.enabled = true;
 MuzzleLight.enabled = true;
 yield WaitForSeconds (0.02);
 muzzleFlash.renderer.enabled = false;
 MuzzleLight.enabled = false;
 }


Zombiebehaviourscript

 #pragma strict
 var zHealth = 3;
 var zspeed = 5;
 var attackTimer = 1;
 var attackRange = 1.5;
 var zdead = false;
 var target : Transform;
 var zTransform : Transform;
 var healthPlayer : healthPlayer;
 var zombie = GameObject;
 
 function Start () {
 
 }
 
 function DestroyThisGameObject()
 {
     Destroy(this.gameObject);
 }
 
 function Update () {
 
 if(zHealth < 1){
 zdead = true;
 }
 if(zdead == true){
 DestroyThisGameObject();
 }
 
 transform.Translate(Vector3.forward*zspeed*Time.deltaTime);
 
 transform.LookAt(target);
 
 //
 ////
 var distance = (target.position - zTransform.position).sqrMagnitude;
 if(distance < attackRange && Time.time > attackTimer){
 healthPlayer.totalHealth -= healthPlayer.damage;
 attackTimer = Time.time + 2;
 Debug.Log ("Hit");
 }
 ////
 //
 
 
 }
 
 
 
 
 
 function FixedUpdate(){
 
 
 
 }



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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Jun 30, 2012 at 08:47 PM

You need the reference of the zombi script attached to the gameobject hit by the ray. You can have it with the raycasthit (you have access to collider and transform, and from there everything). Use GetComponent, and decrease the health.

While I'm at it, you're using 2 identical raycast for no reasons. You only need one. And you need a timer so the player cannot fire continuously.

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

Instantiate JS error that i can't figure out 1 Answer

How to activate a button? 1 Answer

How do I assign a var to the object I've just instantiated? 2 Answers

Gun Script Help 2 Answers

How to assign a Variable with a Raycast 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