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 gearlive · Nov 10, 2013 at 05:52 PM · javascripterrorcontrollerenemybasic

I've got a coding problem please help

im coding in java script and im trying to make my enemy shoot at the first person controller. is this the way to do it if it is then please help with my errors if it isnt please show me how.

 var projectile : DragRigidbody;
 var speed = 50;
 var Player : Transform;
 var Damage = 10;
 var maxHealth = 100;
 var curHealth = 100;
 
  function Update () 
  {
   if(Input.OnTriggerEnter ("Fire1")) {
    clone = Instantiate(projectile, transform.position, transform.rotation); 
    clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
    
      Destroy (clone.gameObject, 10);
      
      if (Player : Collider;
      Damage - maxHealth = curHealth;
     }
      }
     
 
Comment
Add comment · Show 1
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 clunk47 · Nov 10, 2013 at 07:02 PM 0
Share

ummmm... WHAT IS THE ERROR???

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by phxvyper · Nov 10, 2013 at 06:09 PM

The reason why it isn't working is because you destroy the game object right after you instantiate it.

Only destroy it when it collides with something (presumably the player).

e.g.

 if (Input.OnTriggerEnter("Fire1")) {
     close = Instantiate(projectile, transform.position, transform.rotation);
     close.velocity = transform.TransformDirection(Vector3(0, 0, speed));
 }
 
 if (/*Put whatever collision detection flag you have here*/) {
     Destroy(close.gameObject, 10);
     curHealth -= Damage;
 }

I do curHealth -= Damage; instead of what you do in your code because Damage - maxHealth can return a negative, and if you ALWAYS subtract from the max health (unless the maxHealth isnt the Max Health, its actually the player's current health) then the players health will always be either maxHealth or Damage - maxHealth instead of it decreasing whenever the player is shot.

(e.g. take away from the player's current health in order to change it when the player gets shot)

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 clunk47 · Nov 10, 2013 at 07:01 PM 0
Share

He's actually waiting 10 seconds to destroy the instance.

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

Spline Controller (javascript) error 2 Answers

The name 'Player' does not denote a valid type ('not found'). 1 Answer

Basic enemy AI for 2.5D Platormer 1 Answer

I can't script enemy animation 0 Answers

Simple Enemy AI not working : NullReferenceException: Object reference not set to an instance of an object 2 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