I have been working on this RPG for some time now and I have decided to add points and coins to the game that are dropped by an enemy. I have written the script and since it need to collide I have attached the rigidbody and the collider. Coins, points and health points all have the same script attached to them plus an extra one that just adds to the necessary variable. The coins and health points just go through the ground and the normal points just wont go to the player. Here is the script:
var player : GameObject;
var pointGather : GameObject;
var gravity : float;
var canMove : boolean;
var grounded : boolean;
var canGround : boolean;
function Start() {
canGround = true;
grounded = false;
canMove = false;
gravity = 1.2;
player = GameObject.FindWithTag("player");
pointGather = GameObject.FindWithTag("pointGather");
}
function Update() {
if(canMove == false && grounded == false) {
transform.position.y -= gravity * Time.deltaTime;
}
if(transform.position != player.transform.position && canMove == true) {
canGround = false;
grouned = false;
if(transform.position.x > player.transform.position.x) {
transform.position.x -= 1 * Time.deltaTime;
}
if(transform.position.x < player.transform.position.x) {
transform.position.x += 1 * Time.deltaTime;
}
if(transform.position.y > player.transform.position.y) {
transform.position.y -= 0.3 * Time.deltaTime;
}
if(transform.position.y < player.transform.position.y) {
transform.position.y += 0.3 * Time.deltaTime;
}
if(transform.position.z > player.transform.position.z) {
transform.position.z -= 1 * Time.deltaTime;
}
if(transform.position.z < player.transform.position.z) {
transform.position.z += 1 * Time.deltaTime;
}
}
}
function OnTriggerEnter(col: Collider) {
if(col.gameObject == pointGather) {
if(gameObject.tag == "healthPoint" && Player.health < Player.maxHealth) {
canMove = true;
}
if(gameObject.tag == "point" && Player.level < Player.maxLevel) {
canMove = true;
}
if(gameObject.tag == "coin") {
canMove = true;
}
}
if(col.gameObject.tag == "floor" && canGround == true) {
grounded = true;
}
}
function OnTriggerStay(col: Collider) {
if(col.gameObject.tag == "floor") {
if(grounded) {
transform.position.y = col.gameObject.transform.position.y + 0.05;
}
}
}
برچسب:
نویسنده: استخدام کار