Skip to content
Permalink
37f56433d3
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
51 lines (47 sloc) 1.36 KB
using UnityEngine;
using System.Collections;
public class JoyStone : MonoBehaviour
{
#region Field
public int coinValue;
private Vector3 heroPosition;
//private Hero hero;
private Vector3 selfPosition;
private Vector3 distanceToHero;
private float distanceToHeroSqrf;
public float moneySuckRange;
public float jstoneMovingSpeed;
private float moneySuckRangeSqr;
public GameObject hero;
#endregion
// Use this for initialization
void Start ()
{
hero = GameObject.Find("Hero");
//.GetComponent("Hero");
selfPosition = new Vector3 (transform.position.x, 0, transform.position.z);
moneySuckRangeSqr = moneySuckRange * moneySuckRange;
}
// money suck
void Update ()
{
heroPosition = new Vector3 ( hero.gameObject.transform.position.x, 0, hero.gameObject.transform.position.z);
distanceToHero = heroPosition - selfPosition;
distanceToHeroSqrf = distanceToHero.sqrMagnitude;
if( distanceToHeroSqrf <= moneySuckRangeSqr)
{
float amtToMove = jstoneMovingSpeed * Time.deltaTime;
transform.Translate( (heroPosition - selfPosition) * amtToMove,Space.World);
}
}
//when collide with player call player addscore func to gain score
void OnTriggerEnter(Collider otherObject)
{
//Debug.Log ("got hit");
if (otherObject.tag == "Player")
{
otherObject.gameObject.SendMessage("heroGotCoin",coinValue);
Destroy(this.gameObject);
}
}
}