Tuesday, August 3, 2010

Week 3

I created a flow chart for the two enemy script alex asked me to do. When touched by a weapon(shape like a stick), one minion will have its head pop off and the other will disappear and be replaced with items.

The scene is set up with a 2 box(player and enemy). Player will have a stick while the enemy will have a head. My first problem I ran into was learning how to get the collider to work. Which was hard because no one knew what every part of the collider script do so I figured out what can general used for set it up with the enemy. Problem is, I don't know how to script an actual collision script or contactPpoint script. After mind numbing reading and researching, OnTrigger script seem to work best if not easiest for me.

After I set up a collider box for the enemy, I inputed

function OnTriggerEnter (other : Collider) {

if (other.gameObject.tag == "weapon") {

print ("hit")

}

}

That script above will print out "hit" in the debug log so I know it works. Now I have to script the head so it will pick up the hit.

function Update (){

if (playerHit == true){

rigidbody.transform.Translate(Vector3.up * 10 * Time.deltaTime);

}

}

If didn't make the head pop and I couldn't figure out why. I realized the problem was the player hit that the collider script is picking up, isn't being picked up by the head script. I couldn't figured it out right away but I found out that I have to make the variable able to be picked up from other scripts. I ran across a script where someone was using static var so I hope that was it

static var playerHit = false;

that still didn't work. I realized after reading the console that I have to specify where the variable is coming from

in the collider script

function OnTriggerEnter (other : Collider) {

if (other.gameObject.tag == "weapon") {

print ("hit")

enemyHead.playerHit = true;

}

}

In this script I have to state that the playerHit is from the enemyHead script.

With those scripts in place, everything works except the head won't stop floating. I tried adjusting the gravity but ended up messing with the game physics and everything was affected by it. Spent hours on unity3d and no one could pin point the problem or how to work with the gravity specifically for the object. It then occur to me that the jump script in the fps walker does exactly what I needed. I asked Robin if he could help me created and we both looked over the walker script to see what we can learn from it. Robin came up with a script for me that didn't work for me for some reason so I used and wrote my own script from what I learned and able to get the head to pop up and drop. Now the problem is it bounces up and down, non stop.

I added

private var moveDirection = Vector3.zero;

and placed

moveDirection.y -= gravity * Time.deltaTime;

in the update function. this script will need refining for it to work smoothly.

Now I have the enemy replace, item drop script to work on. I used most of the same scene and some of the scripts with some modification. Now understanding how to do this is easy, knowing the scripts to use is a whole different story. Based on my past experience with Unity Script, I have to instantiate an object with the object I want to replace. Prefabbing which was easy.

Same problem I had with the gravity script and moving script, no where tells you the exact function of every part of the script so you know what needs to be replaced and so on.

After researching for an hour, I learned that when instantiating, you will need to var the objects you want to make reference to.

var object1 : GameObject

var object2 : GameObject

*I've seen some put Transform instead of game object with im baffled about.

the instantiate script is

Instantiate(object you want to create, object you want to replace(transform), object you want to replace rotation(rotation);

that script should instantiate for you.

I created a script and collider for the body. With my new understanding how instantiate works.

I set the body script to instantiate the prefab in its location and then destroy itself, revealing the drop item.

var poops : GameObject;

var enemy : gameObject;

function OnTriggerEnter (other : Collider){

if (other.gameObject.tag == "weapon"){

Instantiate(poops, enemy.transform.position, enemy.transform.rotation);

Destroy(enemy);

}

}

And that script should work.

No comments:

Post a Comment