using UnityEngine;
using System.Collections;

public class GenieControlScript : MonoBehaviour {
	
	int LAYER_ENV;
	
	void Start () {
		LAYER_ENV = LayerMask.GetMask(new string[]{"Environment"});
	}
	
	void Update () {
		Rigidbody rb = GetComponent<Rigidbody>();
		
		float ctrl = Input.GetAxis ("Horizontal");
		
		float jump = (Input.GetButtonDown ("Jump")?10:0);
		
		bool bOnGround = Physics.Raycast (transform.position,Vector3.down,0.7f,LAYER_ENV);
		
		if (!bOnGround) 
		{
			ctrl *= 0.2f;
			jump = 0f;
		}
		else if (jump > 1)
		{
			rb.AddForce(new Vector3(0,jump,0),ForceMode.Impulse);
		}
		
		GetComponent<Rigidbody>().AddForce (new Vector3(ctrl*100/(1+GetComponent<Rigidbody>().velocity.magnitude), 0,0));
		GenieAnimScript.bOnGround = bOnGround;
		GenieAnimScript.velocity = GetComponent<Rigidbody>().velocity;
	}
}
