Thursday, January 28, 2016

New Player AI

sing UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerController : MonoBehaviour {


public List<GameObject> bullets;
// Use this for initialization
void Start () {

}

private void shoot (Vector3 startPoint, Vector3 direction)
{
GameObject freeBullet;
foreach(GameObject bullet in bullets)
{
if(bullet.GetComponent<bulletAI>().isFree())
{
bullet.GetComponent<bulletAI>().shoot(startPoint, direction);
break;
}
}

}
// Update is called once per frame
void Update () {


float speed = 10 * Time.deltaTime;
Vector3 pos = this.GetComponent<Transform>().position;

Vector3 mousePoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f ));
mousePoint.y = pos.y;

if(Input.GetKeyDown(KeyCode.Mouse0))
{
shoot(pos, mousePoint);
}

if(Input.GetKey(KeyCode.UpArrow))
{
pos.z += speed;
}
if(Input.GetKey(KeyCode.DownArrow))
{
pos.z -=speed;
}
if(Input.GetKey(KeyCode.RightArrow))
{
pos.x += speed;
}
if(Input.GetKey(KeyCode.LeftArrow))
{
pos.x -= speed;
}
this.GetComponent<Transform>().position = pos;


  this.GetComponent<Transform>().LookAt(mousePoint);

}

public void playerHit(Vector3 hitPoint)
{
Vector3 direction = this.GetComponent<Transform>().position - hitPoint;

this.GetComponent<Rigidbody>().velocity = direction.normalized * 20;
AudioSource source = this.GetComponent<AudioSource>();
source.Play();

//Debug.Log ("Hit");
}

No comments:

Post a Comment