# Instantiate with initial velocity

As a challenge to myself to learn Unity and Unity Scripting I am trying to recreate an old iOS game, Cube Runner (not to sell).

My initial goal has been to set up rudimentary controls for the player, instantiate the basic prefab to dodge, and subsequently set that prefab to head briskly toward the player, on, for instance, the Z axis.

I have complete the first two pieces thus far. I am able to move my basic player around, and I can instantiate my obstacle, however, I am unable to ascertain how to set an initial velocity.

My code thus far (for the instantiation). I realize how utterly simple this script is, but as I said, I am simply trying to learn. I appreciate any and all help.

``````#pragma strict

var newObject : Rigidbody;
private var towerCount = 0;

function Start () {

}

function FixedUpdate ()
{
var objectNum = Random.Range(0,2);

for (var i = 0; i < objectNum; i++)
{
Instantiate(newObject,
Vector3
(
Random.Range(-50.0,50.0),
Random.Range(0.0,0.0),
Random.Range(-50.0,50.0)
),
Quaternion.Euler
(
Random.Range(0.0,0.0),
Random.Range(0.0,360.0),
Random.Range(0.0,0.0)
)
);

Debug.Log("Tower has been built!");
towerCount++;
}
}
``````

The problem with newObject.AddForce(1,0,0) is that is tries adding that force to your prefab and not the object that you have just created.
To fix this, assign the instantiated object to a variable:

``````        var objectClone = Instantiate(newObject,
Vector3
(
Random.Range(-50.0,50.0),
Random.Range(0.0,0.0),
Random.Range(-50.0,50.0)
),
Quaternion.Euler
(
Random.Range(0.0,0.0),
Random.Range(0.0,360.0),
Random.Range(0.0,0.0)
)
);
``````

To move that prefab you should make a new script for it completely(this is an AI script in essence), regardless of if it moves in a steady direction or all around. So start by making a TowerMove script, if you havent alreadyâ€¦make a prefab of this tower and add the newly created script to it. Drag that tower prefab onto the public variable on the script shown above. On this current script in the instantiate function add

``````// Same code except with a var whatEver infront for reference
var instance=Instantiate(newObject,
Vector3
(

//Now you can call that new object using the var whatEver
//and add the player(gameobject this script is attached to) to
//the TowerMove's player variable
instance.GetComponent(TowerMove).player=this.gameObject;
``````

Now for the TowerMove script

``````On TowerMove

public var player:Transform;
private var direction:Vector3;

function Start()
{
direction=(player.position-transform.position).magnitude;
}
function Update()
{
transform.Translate(direction*Time.deltaTime);
}
``````

Takes one direction on start of the script(when prefab is spawned) in direction of the player.

I actually ended up going a slightly different route with my code. As seen below.

Creation Script

``````#pragma strict

var newTower : Rigidbody;
private var towerCount = 0;

function Start () {}

function FixedUpdate ()
{
var towerNum = Random.Range(0,2);

for (var i = 0; i < towerNum; i++)
{
var towerSpawnPosition = Instantiate
(newTower,
Vector3
(
Random.Range(-50,50),
Random.Range(0,0),
Random.Range(180,200)
),
Quaternion.Euler
(
Random.Range(0,0),
Random.Range(0,0),
Random.Range(0,0)
)
);

Debug.Log("Tower has been built!");
towerCount++;
}
}
``````

Movement Script

``````#pragma strict

public var player:Transform;
private var direction:Vector3;

function Start()
{
direction = transform.TransformDirection(0,0,-20);
}
function Update()
{
transform.Translate(direction*Time.deltaTime);
}
``````