Permalink
Cannot retrieve contributors at this time
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?
Silverkeep/Guardian of Silverkeep/Assets/Scripts/JoyStone.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
51 lines (47 sloc)
1.36 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |