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 /
This question was closed Feb 09, 2014 at 03:41 AM by DaveA for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MartinTV · Feb 09, 2014 at 03:21 AM · c#getcomponent

How am I supposed to use GetComponent?

I hate to ask this question especially when it has been asked so many times, but I just don't get how get component works. All the other threads I've been to haven't helped in the slightest.

What I am trying to do is make an a fireball deal damage to another object when it collides with it. Usually I would use sendmessage, but here the player can charge up their fireball and make it do increased damage.

SO what I want to do is have the fireball directly take away the object's health based on it's charge level (if the object has a health variable).

 using UnityEngine;
 using System.Collections;
 
 public class FireBall_beh : MonoBehaviour
 {
     // Gameplay stuff
     public static float chargeLevel = 100f;
     public float speed = 12f;
     public float turnRate = 3f;
     public float lifeTime = 3f;
     private GameObject target;
     private Vector3 rayDir;
     
     // Charge level stuff
     private float particleStartSpeed;
     private float particleEmissionRate;
     private float particleRadius;
     public float baseDamage = 15f;
 
 
     void OnEnable ()
     {
         // change size depending on the charge level
         particleStartSpeed = particleSystem.startSpeed;
         particleEmissionRate = particleSystem.emissionRate;
         particleRadius = transform.localScale.y;
         
         particleSystem.startSpeed = (particleStartSpeed / 100) * chargeLevel;
         particleSystem.emissionRate = (particleEmissionRate / 100) * chargeLevel;
         transform.localScale = transform.localScale = new Vector3((particleRadius / 100) * chargeLevel, (particleRadius / 100) * chargeLevel, (particleRadius / 100) * chargeLevel);
         
         // Die after an ammount of time
         Invoke("Die", lifeTime);
                 
         // Do a sphere cast to see if there are players to lock onto
         RaycastHit hit;
         rayDir = transform.forward;
         if(Physics.SphereCast(transform.position, 10, rayDir, out hit, 20f))
         {
             //Debug.Log(hit.collider.name);
             
             if(hit.collider.tag == "Player")
             {
                 target = hit.collider.gameObject;
             }
         }
     }
 
 
     void FixedUpdate ()
     {
         // Move forward
         transform.Translate(0,0, speed * Time.deltaTime);
         
         // If this object has a target
         if(target != null)
         {
             // Check if the target is infront
             Vector3 targetDir = target.transform.position - transform.position;
             float angle = Vector3.Angle(targetDir, transform.forward);
             // Follow the target
             if(angle >= -90f && angle <= 90f )
             {
                 Vector3 lookTarget = target.transform.position - transform.position;
                 Quaternion rotation = Quaternion.LookRotation(lookTarget);
                 transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * turnRate);
             }
         }
     }
     
     void  OnTriggerEnter(Collider other) 
     {
         // Calculate damage
         float damage = (baseDamage / 100) * chargeLevel;
         // Get the script of the other object and take away it's health (if it has a health variable)
     
         // Die
         Die();
 
     }
         
     void Die()
     {
         // Change particel system paramaters back to normal
         particleSystem.startSpeed = particleStartSpeed;
         particleSystem.emissionRate = particleEmissionRate;
         transform.localScale = new Vector3(particleRadius, particleRadius, particleRadius);
         // Clear target
         target = null;
         // Deactivate
         gameObject.SetActive(false);
         
     }
 }

The chargeLevel variable is a percentage 100 being how it is normally and it increases to 300% damage and size (this variable is set in the player script when the player fires it)

Thanks for all and any help and if there is a better way to do this please let me know.

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

  • Sort: 
avatar image
0

Answer by MartinTV · Feb 09, 2014 at 03:40 AM

I think I have just come up with a much simpler way to do this, that was kind of obvious, so don't pay attention to this hahah.

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

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to get a variable value from another script(C#)? 1 Answer

Distribute terrain in zones 3 Answers

insert script question 1 Answer

Using GetComponent in multiple scripts for same component? C# 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