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 herp johnson · Aug 14, 2012 at 08:31 PM · cloneaddingmissile

adding script to a clone

I've got a missile script that uses a few other scripts (like an explosion and MoveObject script), but I can't figure out how to add, access, and use them through this script. Take a look at my code, what do you think I'm doing wrong?

p.s. don't question the use of constantforce here, it's just for realism.

Condensed Version (the main problem areas--just ask if you want to see the rest)

 //needed to access script MoveObject, it must be attached to a GO
 var emptyGO : GameObject; 
 var moveObject : MoveObject = emptyGO.AddComponent(MoveObject); 
 
 //needed to add explosionPrefab to the missile
 public var explosionPrefab : Transform; 
 var missileCloneScript = instantiatedProjectile.AddComponent(MissileClone);
 missileCloneScript.explosionPrefab = explosionPrefab;
 
 //this is what I'm trying to use for movement but I can't figure out how to access the script through mine
 if (seek)
 yield moveObject.use.Translation(instantiatedProjectile.transform, target.position, 2.5, MoveType.Speed);


Full Version (comments provide some more detail)

 #pragma strict
 var projectile : GameObject; //the actual GO of the missile, whose rigidbody is used for
   //instantiatedProjectile, and then is destroyed once launched.
 
 //Pod, used for the start location of instantiatedProjectile. don't really need.
 var Pod : Transform;
 
 //boolean used for the firing of missile
 private var missilefire : boolean = true;
 
 //boolean used for the seeking action of missile
 private var seek : boolean = false; 
 
 //instantiated missile clone
 private var instantiatedProjectile : GameObject;
 
 //needed to access script MoveObject, it must be attached to a GO
 var emptyGO : GameObject; 
 var moveObject : MoveObject = emptyGO.AddComponent(MoveObject); 
 
 //needed to add explosionPrefab to the missile
 public var explosionPrefab : Transform; 
 var missileCloneScript = instantiatedProjectile.AddComponent(MissileClone);
 missileCloneScript.explosionPrefab = explosionPrefab;
 
 
 var target : Transform; //miscellaneous variables
 var smooth = 0.1;
 var reloadTime = 0.5;
 var ammoCount = 8;
 var lastShot = -10.0;
 
 
 if (seek)
 yield moveObject.use.Translation(instantiatedProjectile.transform, target.position, 2.5, MoveType.Speed);
 // ^this is what I'm trying to use but I can't figure out how to access the script
 
 
 function Update () {
 
 //stopping the y-motion effect that gravity created
  if (instantiatedProjectile.rigidbody.velocity.z > 10)
  {
  instantiatedProjectile.rigidbody.constantForce.force = Vector3(0,0,0);
      instantiatedProjectile.rigidbody.velocity.y = 0;
         seek = true;
  }
 }
 
 
 
 
 function FixedUpdate () {
 
 //missile tracking process
  if (seek)
  {
  //instantiatedProjectile.transform.position = Vector3.MoveTowards (instantiatedProjectile.transform.position, target.position,Time.deltaTime * 5);
  
  // ^ This worked, but when the missile got about 10 units close to the target, 
  //   it seemed to repel and started moving away.
  
  instantiatedProjectile.transform.LookAt(target);
  }
 }
 
 
 
 function Fire () 
 {
  if (Time.time > reloadTime + lastShot && ammoCount > 0) 
  {
  //creating a firing sequence, instantiating clones, and destroying GOs/adding gravity for realism
   if (missilefire)
         {   
             instantiatedProjectile = Instantiate (projectile, Pod.position, transform.rotation);
             Destroy (projectile);
            
             instantiatedProjectile.rigidbody.useGravity = true;
             yield WaitForSeconds(0.5);
             instantiatedProjectile.rigidbody.useGravity = false;
             instantiatedProjectile.rigidbody.constantForce.force = (Vector3.forward*10);
             yield WaitForSeconds(reloadTime);
             missilefire = false;
         }    
  
  //Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider); 
  //not really needed
  
  lastShot = Time.time;
  ammoCount--;
  }
 }
Comment
Add comment · Show 11
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 sman_47o · Aug 14, 2012 at 08:43 PM 0
Share

can we have the rest?

avatar image herp johnson · Aug 14, 2012 at 08:45 PM 0
Share

sure thing. i'll put it below the shortened version.

avatar image sman_47o · Aug 14, 2012 at 08:50 PM 0
Share

sweet thanks

avatar image herp johnson · Aug 14, 2012 at 08:55 PM 0
Share

so what do you think is wrong with it? it's not putting the script on the cloned object, so it must be something with my declarations.

avatar image sman_47o · Aug 14, 2012 at 08:57 PM 0
Share

why can't the instantiated prefab already have those scripts attached to begin with? i think you may be making things more complicated than they need to be.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sman_47o · Aug 14, 2012 at 09:17 PM

heres my answer that will definitely streamline your code and more than likely fix it. you need to attach all those scripts to the object instead of adding them on runtime. add the projectile gameObject to your project files and instead of having to destroy it you will just reference it every time you spawn a missile.

also you could get rid of having a whole other script to move the missile why don't you write one inside of the script that you posted and simply call a function that sets the rigidBodies also to stop the downward movement caused by gravity use rigidbody.UseGravity(false);

Comment
Add comment · Show 8 · 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 herp johnson · Aug 14, 2012 at 09:24 PM 0
Share

the reason I haven't put it in project files is because there's 8 individual missile GameObjects and I wanted to put this script on them. but in that case, why don't I just forget about instantiating and just manipulate the GameObjects themselves? facepalm

avatar image sman_47o · Aug 14, 2012 at 09:26 PM 0
Share

you could do that haha but instantiate makes a clone so you can have several instances of the same object in the game XD

avatar image sman_47o · Aug 14, 2012 at 09:31 PM 0
Share

so what are you trying to do? only allow for eight shots? and I'm right in thinking that this script is inside of the missile object right?

avatar image herp johnson · Aug 14, 2012 at 09:42 PM 0
Share

I originally used $$anonymous$$oveTowards, but the missile wouldn't actually hit the target, it'd get close and then repel away. I also just now took out transform.LookAt and threw a SmoothLookAt script on the missile GO (game object) for more functionality.

Um, yeah, it's just a short educational (but action-packed!) game I'm making, it doesn't have to have over 8 shots and a reload process or anything. it'd be nice though, perhaps I can work on that later.

Currently, my objective is to make it so that each time space is pressed, it fires a missile from a different location on the pylon(the thing holding the missiles). I want to get the tracking and explosion down first though.

avatar image sman_47o · Aug 14, 2012 at 09:46 PM 0
Share

well if you just instantiate the object you could hit two birds with one stone, make a smoother running game and it will be easier to implement the reload system later.

Show more comments
avatar image
0

Answer by herp johnson · Aug 14, 2012 at 09:45 PM

Ok here's MoveObject. for more info on it go Here

enum MoveType {Time, Speed} static var use : MoveObject;

 function Awake () {
  if (use) {
  Debug.LogWarning("Only one instance of the MoveObject script in a scene is allowed");
  return;
  }
  use = this;
 }
  
 function Translation (thisTransform : Transform, endPos : Vector3, value : float, moveType : MoveType) {
  yield Translation (thisTransform, thisTransform.position, thisTransform.position + endPos, value, moveType);
 }
  
 function Translation (thisTransform : Transform, startPos : Vector3, endPos : Vector3, value : float, moveType : MoveType) {
  var rate = (moveType == MoveType.Time)? 1.0/value : 1.0/Vector3.Distance(startPos, endPos) * value;
  var t = 0.0;
  while (t < 1.0) {
  t += Time.deltaTime * rate;
  thisTransform.position = Vector3.Lerp(startPos, endPos, t);
  yield; 
  }
 }
  
 function Rotation (thisTransform : Transform, degrees : Vector3, time : float) {
  var startRotation = thisTransform.rotation;
  var endRotation = thisTransform.rotation * Quaternion.Euler(degrees);
  var rate = 1.0/time;
  var t = 0.0;
  while (t < 1.0) {
  t += Time.deltaTime * rate;
  thisTransform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
  yield;
  }
 }


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 sman_47o · Aug 16, 2012 at 12:19 AM 0
Share

Hey im sorry I never got back to you the other day I lost power and I'm currently s$$anonymous$$ling Internet from my neighbor XD I'll try and get something written from my iPod. But if i can't I can at least point you in the right direction. Use the lookAt function to get your missile (which if i were you id make kinematic so that gravity wont effect it) to look at your target and then set its velocity to transform.forward * speed (speed would be a float that you can fiddle around with to make it do what you want) that's all you really need to move your object. (and if velocity gives you crap, it loves to be difficult, you can also try using translate, add relative force, or position but use velocity if at all possible). Good luck to you and I hope I can some power back soon after they deal with this tree) the world needs educational games with explosives I would have love phonics if I had gotten the reward of demolishing something.

avatar image herp johnson · Aug 16, 2012 at 12:34 AM 0
Share

actually I got rid of the instantiation altogether. it was too much trouble. unity reccomends not using velocity so i used Vector3.$$anonymous$$oveTowards, which is more dynamic and doesn't screw you over when using colliders. now everything works! :D

avatar image sman_47o · Aug 16, 2012 at 01:36 AM 0
Share

Sweet I'm glad you got it going :)

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

trouble adding script to clone object 1 Answer

Code only affects first clone? 2 Answers

Cloning Problem Script 0 Answers

How do I assign a script to a clone through another script? 1 Answer

Duplicate Object, by dragging? (SporeCreatureCreator Like) 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