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 KnC_Studios · Aug 09, 2013 at 08:39 PM · targettargetingrocket

Rocket launcher script help

Hi, I have a fps and I have an enemy robot attack any object that has the tag player. (other objects have the tag player as well) I have a rocket launcher object and script that are children of the robot. Here is the first script:

RocketLauncher.js

    #pragma strict
     var projectile : Rigidbody;
     var initialSpeed = 20.0;
     var reloadTime = 0.5;
     var ammoCount = 20;
 
     private var lastShot = -10.0;
     
     private var cc : CharacterController; 
     var player : Transform;
      
     function Start() {
         //var go = GameObject.Find("First Person Controller");
         var go = player;
         go = GameObject.FindWithTag("Player").transform;
         player = go.transform;
         cc = go.GetComponent(CharacterController);
      }
      
      
      
     function Fire () {
         // Did the time exceed the reload time?
         if (Time.time > reloadTime + lastShot && ammoCount > 0) {
            // create a new projectile, use the same position and rotation as the Launcher.
            var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
      
            // Time of flight to reach player
            var t = (player.position - transform.position).magnitude / initialSpeed;
      
            // Project future position
            var futurePos = player.position + cc.velocity * t;
      
            // Aim and fire at that future position
            var aim = (futurePos - transform.position).normalized;
            instantiatedProjectile.transform.rotation = Quaternion.LookRotation(aim);
            instantiatedProjectile.velocity = aim * initialSpeed;
      
            // Ignore collisions between the missile and the character controller
            Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
      
            lastShot = Time.time;
            ammoCount--;
         }
     }

It tracks the target the points the rocket the right way then fires. It works fine until the robot kills target which is the same object that is assigned to the player variable. Then the rockets don't go anywhere and i get an error. I want the robot to send its target value to the launchers target value. Can someone please post a revision of the code? Thank you in advance.

Comment
Add comment · Show 2
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 KnC_Studios · Aug 10, 2013 at 06:35 PM 0
Share

I'm going to bump.

avatar image KnC_Studios · Aug 18, 2013 at 01:18 AM 0
Share

Bumping, again.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Aug 18, 2013 at 01:29 AM

Assuming you are targeting any object that has the tag 'Player', you just need to find the 'player' transform each time you need to fire:

     #pragma strict
     var projectile : Rigidbody;
     var initialSpeed = 20.0;
     var reloadTime = 0.5;
     var ammoCount = 20;
  
     private var lastShot = -10.0;
  
     private var cc : CharacterController; 
     var player : Transform;
  
     function Start() {
         cc = go.GetComponent(CharacterController);
      }
 
     function Fire () {
         // Did the time exceed the reload time?
         if (Time.time > reloadTime + lastShot && ammoCount > 0) {
             var go = GameObject.FindWithTag("Player")
             
             if (go == null) {
                 return;
             }
             player = go.transform;
            // create a new projectile, use the same position and rotation as the Launcher.
            var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
  
            // Time of flight to reach player
            var t = (player.position - transform.position).magnitude / initialSpeed;
  
            // Project future position
            var futurePos = player.position + cc.velocity * t;
  
            // Aim and fire at that future position
            var aim = (futurePos - transform.position).normalized;
            instantiatedProjectile.transform.rotation = Quaternion.LookRotation(aim);
            instantiatedProjectile.velocity = aim * initialSpeed;
  
            // Ignore collisions between the missile and the character controller
            Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
  
            lastShot = Time.time;
            ammoCount--;
         }
     }
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 KnC_Studios · Aug 19, 2013 at 02:20 AM 0
Share

This doesn't work right. It keeps on giving me an error saying that the target was destroyed.

avatar image robertbu · Aug 19, 2013 at 06:50 AM 0
Share

I made a change that will fix the condition where there are no Players to be found. This may be your problem

Note I cannot easily run and test this code, so I have to depend you to tell me if something does not work right. And I need complete information including the exact error in the condole and a full description of the behavior.

avatar image KnC_Studios · Aug 21, 2013 at 02:46 PM 0
Share

Thank you.

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

14 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

Related Questions

Homing Missile not maintaining forward velocity 1 Answer

Floor Halo Target System 0 Answers

Track objects in camera view using GUI? Like a HUD for flight sim lockon targeting. 1 Answer

Targeting an enemy trouble 1 Answer

Circle strafing/Z-Targeting 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