public class PlayerOne : MonoBehaviour {
public GameObject ball;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 pos = GetComponent<Transform>().position;
if(Input.GetKey (KeyCode.D))
{
pos.z += 0.1f;
}
if(Input.GetKey (KeyCode.A))
{
pos.z -= 0.1f;
}
if(Input.GetKey (KeyCode.W))
{
pos.x -= 0.1f;
}if(Input.GetKey (KeyCode.S))
{
pos.x += 0.1f;
}
GetComponent<Transform>().position = pos;
if(Input.GetKeyDown(KeyCode.G) && ball != null)
{
Vector3 ballPos = ball.GetComponent<Transform>().position;
float distance = Vector3.Distance (ballPos, pos);
if(distance <1.5f)
{
Vector3 direction = ballPos - pos;
direction.Normalize();
ball.GetComponent<Rigidbody>().AddForce(direction * 100);
}
}
}
}
No comments:
Post a Comment