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 camtronius · Jun 24, 2014 at 04:47 AM · rotationphysicsquaterniongravitydistance

make projectiles always hit target

Hey guys, Making a 3d TD game where I have one of my towers shooting a projectile to hit an enemy unit. Since the enemy units are moving the projectile doesnt hit its target every time. I heard this can be remedied by using the speed and direction of the enemy. I dont know how I would include this though. I dont think it would hit if the enemy changed direction either. Does anyone know of a way I could solve this problem? Code is posted below. Thanks in advance,

 AstarAIEnemy enemyObjectDetection = EnemySpawn.enemyList[closestIndex].GetComponent<AstarAIEnemy> (); //gets astarai code for that unit
                     
                     targetPosition = enemyObjectDetection.transform.position; //sets the enemy position to the pos of the player unit
                     
                     float distanceToTargetPos = Vector3.Distance (targetPosition, transform.position); //finds distance between the two units

                     //projectile motion code

                         //    while (true) {
                                 
                                 Projectile.position = this.transform.position; //puts projectile at archers pos


                                 //calc velocity
                                 float projectile_Velocity = distanceToTargetPos / (Mathf.Sin (2 * firingAngle * Mathf.Deg2Rad) / gravity);
                                 
                                 // Extract the X  Y componenent of the velocity
                                 float Vx = Mathf.Sqrt (projectile_Velocity) * Mathf.Cos (firingAngle * Mathf.Deg2Rad);

                                 float Vy = Mathf.Sqrt (projectile_Velocity) * Mathf.Sin (firingAngle * Mathf.Deg2Rad);
                                 
                                 // Calculate flight time.
                                 float flightDuration = distanceToTargetPos / Vx;
                                 
                                 Projectile.rotation = Quaternion.LookRotation(targetPosition - Projectile.position); //rotates player/projectile towards object 
                                 
                                 while (elapse_time <= flightDuration) {

                                 Projectile.Translate (0, (Vy - (gravity * elapse_time)) * Time.deltaTime,  Vx * Time.deltaTime); 
                                     
                                     elapse_time += Time.deltaTime;
                                     
                                     yield return null;    
                                     
                                 }

                                 //yield return new WaitForSeconds(5f);
                                 
                                 Projectile.position = this.transform.position; //resets so that unit can fire again
                                 elapse_time = 0;




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
Best Answer

Answer by ABerlemont · Jun 24, 2014 at 09:04 AM

If you want your missile to "follow" your target wherever it goes you can do this :

 using UnityEngine;
 using System.Collections;
 
 public class Missile : MonoBehaviour {
 
   public Transform target;
   float speed = 2f;
   void Update(){
     if(target == null)  return;
 
     transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * speed);
   }
   
 }

EDIT

Another way to do it.

This one is timed based instead of speed based so that's it's easier to control height.

 using UnityEngine;
 using System.Collections;
 
 public class Missile : MonoBehaviour {
   
   public Transform target;
 
   float timeTravel = 10f;
   float time = 0f;
 
   Vector3 origin;
   Vector3 destination;
   float distance = 0f;
 
   void Start(){
     shootAt(target);
   }
 
   void shootAt(Transform newTarget){
     target = newTarget;
 
     time = timeTravel; // in seconds
 
     origin = transform.position;
     destination = target.position;
   }
 
   void Update(){
     if(target == null)  return;
 
     time -= Time.deltaTime;
 
     destination = target.position;
     float progress = Mathf.InverseLerp(timeTravel, 0f, time);
 
     //set projectile position
     Vector3 newPosition = Vector3.Lerp(origin, destination, progress);
 
     //set projectile height
     newPosition.y = Mathf.Cos(Mathf.Lerp(-Mathf.PI * 0.5f, Mathf.PI * 0.5f, progress));
 
     transform.position = newPosition;
     if(transform.position == destination){
       target = null;
     }
   }
   
 }

Comment
Add comment · Show 3 · 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 camtronius · Jun 24, 2014 at 05:02 PM 0
Share

@ABerlemont This would work but I also need it to arc like it was a projectile. Not just a missle.

avatar image ABerlemont · Jun 27, 2014 at 12:44 PM 0
Share

I edited my answer to have the arc thing. To change height just multiply the newPosition.y before applying it to the object's position.

avatar image camtronius · Jun 27, 2014 at 03:01 PM 0
Share

@ABerlemont I used your movetowards code with my existing physics code for the y component of velocity to get an answer. I had to put the target position inside the while loop so that it updates the enemy position while its traveling. Thanks for your help, ill give you the answer.

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

23 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

Related Questions

How to make any dice have a given face(int) face up? 1 Answer

Orbit cam and Rigidbodies aiming 0 Answers

rotation and gravity control 1 Answer

Game Object rotation sometimes doesn't match defined quaternion 0 Answers

Skateboard Tricks - Rotation Options? 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