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 Sean 12 · Mar 27, 2011 at 09:00 PM · itweenpathmissilehomingputonpath

How to create homing missile that follows path using Unity and iTween

Greetings everyone!

Been messing around with Unity and iTween and bought some of the examples and have a character moving along a path and jumping all fine and dandy.

Problem arose when I decided to have the character shoot. Originally I used a modified version of the code from FPS tutorial, but that just fires in a direction. What I want is for the projectile to be created and then follow down the same path the player is on but I'm having problems getting the syntax right for the iTween.PutOnPath. Specifically, with getting the players current position.

Here's the current code and any help would be greatly appreciated.

var projectile : GameObject; var initialSpeed = 20.0; var reloadTime = 0.5; var ammoCount = 20; private var lastShot = -10.0;

//Path Attach var path : Transform[]; private var pathPosition : float=0 ; public var character : Transform; private var value : float; public var rocketSpeed : float; private var closest : GameObject; private var coordinateOnPath : Vector3; var pathPercent : float;

function Update(){ FindClosestEnemy(); DetectKeys(); }

// Find the name of the closest enemy function FindClosestEnemy () : GameObject {

// Find all game objects with tag Enemy var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Enemy"); var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var go : GameObject in gos) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest; }

function DetectKeys(){ //fire if (Input.GetKeyDown("left ctrl") && Time.time > reloadTime + lastShot && ammoCount > 0) { Debug.Log ("FIRE"); // create a new projectile, use the same position and rotation as the Launcher. var instantiatedProjectile : GameObject = Instantiate (projectile,transform.position, transform.rotation);

   //Find Character Location on Path
   pathPercent = pathPosition%1;
   coordinateOnPath = (iTween.PointOnPath(path,pathPercent));

   //Place on Path
   iTween.PutOnPath(projectile,path, coordinateOnPath);

   //Orient object to path 
   transform.LookAt(iTween.PointOnPath(path,value+.05));

   //Make it a RigidBody
   instantiatedProjectile.AddComponent ("Rigidbody");
   instantiatedProjectile.AddComponent ("CapsuleCollider");

   // Move Rocket to Closest Enemy
   iTween.MoveTo(closest, transform.position, rocketSpeed);

   //Original Movement Code
   //instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

   //Ignore Collisions between object and Projectile
   Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);

   //Cooldown and Reduce Ammo Count
   lastShot = Time.time;
   ammoCount--;

 }

}

//Just making your code better looking, sorry for the edit.

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 Sean 12 · Mar 29, 2011 at 01:52 AM 0
Share

Thanks for the cleanup. Any idea on how to fix it so that it not only looks pretty but works?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by luozitian · Jan 10, 2012 at 10:32 AM

As my undertanding, function MoveTo of itween works only on start instead of update,so, that is the problem. my suggestion is to have on script to create prefab that move by itween and you need create path on runtime then it may get the effect you need

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 Sanosake1 · Sep 12, 2012 at 07:28 AM 0
Share

would this help? http://itween.pixelplacement.com/documentation.php#$$anonymous$$oveUpdate

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

2 People are following this question.

avatar image avatar image

Related Questions

Go back to first node (itween + moveto) 2 Answers

itween MoveTo IndexOutOfRangeException 0 Answers

iTween Camera Jitter while moving on path 2 Answers

How to move an object on a random path with iTween 1 Answer

Help With Homing Missiles - Annoying! 3 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