• 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 RgClube · Oct 15, 2020 at 04:29 PM · instantiateprojectiledestroy object

How to create detonation time for grenade projectile.

Hello everyone, I have problems with setting the time of detonation and destruction of the grenade projectile. Since the enemy creates grenades with a cooldown that is shorter than the detonation time, as you might guess, the next instance of the projectile will set the default detonation time. Luckily, I was able to fix this problem by setting the detonation time in the launch function, but with this approach, when the detonation time reaches 0, only the last grenade created will be destroyed. Any help is appreciated! Code, it's a little messy, but you need the start and fixUpdate functions:

 private AttackDetails attackDetails;
     public GameObject player;
 
     [Tooltip("Position we want to hit")]
     public Vector3 targetPos;
     
     [Tooltip("Horizontal speed, in units/sec")]
     public float speed = 25;
     
     [Tooltip("How high the arc should be, in units")]
     public float arcHeight = 6;
     private Vector3 nextPos;
     PlayerMovement playerMovement;
     Rigidbody2D rb;
     private bool arrowHitPlayerPos = false;
     Vector3 startPos;
     private bool hasHitGround;
     Vector2 velocityVector;
     [SerializeField]
     private LayerMask whatIsGround;
     [SerializeField]
     private LayerMask whatIsPlayer;
     [SerializeField]
     private Transform damagePosition;
     [SerializeField]
     private float damageRadius;
     private float timeOfDetonation = 3;
     private float explosionRadius = 2;
     Collider2D damageHit;
     void Start() {
         // Cache our start position, which is really the only thing we need
         // (in addition to our current position, and the target).
         startPos = transform.position;
         playerMovement = FindObjectOfType<PlayerMovement>();
         rb = GetComponent<Rigidbody2D>();
         targetPos = playerMovement.groundPlayerPosVector;
         timeOfDetonation = 3;
         Debug.Log(timeOfDetonation);
         
     }
     
     void Update() {
         // Compute the next position, with arc added in
         
         Debug.Log(timeOfDetonation);
         float x0 = startPos.x;
         float x1 = targetPos.x;
         float dist = x1 - x0;
         float nextX = Mathf.MoveTowards(transform.position.x, x1, speed * Time.deltaTime);
         float baseY = Mathf.Lerp(startPos.y, targetPos.y, (nextX - x0) / dist);
         float arc = arcHeight * (nextX - x0) * (nextX - x1) / (-0.25f * dist * dist);
         nextPos = new Vector3(nextX, baseY + arc, transform.position.z);
         velocityVector = nextPos - transform.position;
         timeOfDetonation -= Time.deltaTime;
         
         
         // Rotate to face the next position, and then move there
         transform.rotation = LookAt2D(nextPos - transform.position);
         if(hasHitGround != true || rb.velocity.y <=0)
         rb.velocity = velocityVector * speed;
         
     }
     private void FixedUpdate()
     {
         if (!hasHitGround)
         {
             if(timeOfDetonation <=0)
             {
                 Collider2D damageHit = Physics2D.OverlapCircle(damagePosition.position, explosionRadius, whatIsPlayer);
                 Destroy(gameObject);
             }
             
             Collider2D groundHit = Physics2D.OverlapCircle(damagePosition.position, damageRadius, whatIsGround);
 
             if (damageHit)
             {
                 damageHit.transform.SendMessage("Damage", attackDetails);
                 
             }
 
             if (groundHit)
             {
                 hasHitGround = true;
             }
         }        
     }
     
      private void OnDrawGizmos()
     {
         Gizmos.DrawWireSphere(damagePosition.position, damageRadius);
         Gizmos.DrawWireSphere(damagePosition.position, explosionRadius);
     }
     /// 
     /// This is a 2D version of Quaternion.LookAt; it returns a quaternion
     /// that makes the local +X axis point in the given forward direction.
     /// 
     /// forward direction
     /// Quaternion that rotates +X to align with forward
     static Quaternion LookAt2D(Vector2 forward) {
         return Quaternion.Euler(0, 0, Mathf.Atan2(forward.y, forward.x) * Mathf.Rad2Deg);
     }

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

168 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error CS0029 - Trying to destroy an object and then instantiate it again? 1 Answer

Following Object can't find newly Instantiated Object (Solved) 1 Answer

My instantiated projecttile refuses to move 2 Answers

Bullet instantiates sometimes in wrong posiiton 0 Answers

Respawning Single Enemy at a Random Range 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