x


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--;
 }
}
more ▼

asked Aug 14 '12 at 08:31 PM

herp johnson gravatar image

herp johnson
56 4 8 13

can we have the rest?

Aug 14 '12 at 08:43 PM sman_47o

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

Aug 14 '12 at 08:45 PM herp johnson

sweet thanks

Aug 14 '12 at 08:50 PM sman_47o

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.

Aug 14 '12 at 08:55 PM herp johnson

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.

Aug 14 '12 at 08:57 PM sman_47o
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);

more ▼

answered Aug 14 '12 at 09:17 PM

sman_47o gravatar image

sman_47o
81 1 3 4

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

Aug 14 '12 at 09:24 PM herp johnson

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

Aug 14 '12 at 09:26 PM sman_47o

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?

Aug 14 '12 at 09:31 PM sman_47o

I originally used MoveTowards, 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.

Aug 14 '12 at 09:42 PM herp johnson

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.

Aug 14 '12 at 09:46 PM sman_47o
(comments are locked)
10|3000 characters needed characters left

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;
 }
}
more ▼

answered Aug 14 '12 at 09:45 PM

herp johnson gravatar image

herp johnson
56 4 8 13

Hey im sorry I never got back to you the other day I lost power and I'm currently stealing 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.

Aug 16 '12 at 12:19 AM sman_47o

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

Aug 16 '12 at 12:34 AM herp johnson

Sweet I'm glad you got it going :)

Aug 16 '12 at 01:36 AM sman_47o
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3419
x135
x47
x42

asked: Aug 14 '12 at 08:31 PM

Seen: 484 times

Last Updated: Aug 16 '12 at 01:36 AM