枪口 与子弹旋转同样角度。
离子系统与子弹 拖入地底下隐藏。
坦克:分5格动画,绑定脚本optplayer
炮塔:绑定脚本optpaota
子弹:动态绑定脚本zidanfei addcomponent
枪口:创建一个空对象
爆炸:离子系统
opt坦克脚本:
using UnityEngine;
using System.Collections;
public class optPlayer : MonoBehaviour
{
public Animation player;
private float speed = 15;
private float angleY = 0;
private GameObject bullet;
private GameObject qiangkou;
// Use this for initialization
void Start()
{
player.Play("stand");
bullet = GameObject.Find("zidan");
qiangkou = GameObject.Find("qiangkou");
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
player.Play("forward");
}
else if (Input.GetKeyUp(KeyCode.W))
{
player.Play("stand");
}
if (Input.GetKeyDown(KeyCode.S))
{
player.Play("back");
}
else if (Input.GetKeyUp(KeyCode.S))
{
player.Play("stand");
}
if (Input.GetKeyDown(KeyCode.A))
{
player.Play("left");
}
else if (Input.GetKeyUp(KeyCode.A))
{
player.Play("stand");
}
if (Input.GetKeyDown(KeyCode.D))
{
player.Play("right");
}
else if (Input.GetKeyUp(KeyCode.D))
{
player.Play("stand");
}
if (Input.GetKey(KeyCode.W))
{
this.transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
else if (Input.GetKey(KeyCode.S))
{
this.transform.Translate(Vector3.back * Time.deltaTime * speed);
}
else if (Input.GetKey(KeyCode.A))
{
this.transform.Translate(Vector3.left * Time.deltaTime * speed);
}
else if (Input.GetKey(KeyCode.D))
{
this.transform.Translate(Vector3.right * Time.deltaTime * speed);
}
if (Input.GetMouseButton(0))
{
angleY = Input.mousePosition.x;
this.transform.localEulerAngles = new Vector3(this.transform.localEulerAngles.x, angleY, this.transform.localEulerAngles.z);
//print(Input.mousePosition);
}
}
void LateUpdate()
{
if (Input.GetMouseButtonDown(1))
{
GameObject zidan = (GameObject)GameObject.Instantiate(bullet);
zidan.transform.position = qiangkou.transform.position;
zidan.transform.rotation = qiangkou.transform.rotation;
zidan.AddComponent("Rigidbody");
Rigidbody rigBody = (Rigidbody)zidan.GetComponent("Rigidbody");
rigBody.useGravity = false;
zidan.AddComponent("zidanfei");
}
}
}
opt炮塔脚本:
using UnityEngine;
using System.Collections;
public class optpaota : MonoBehaviour
{
public GameObject target = null;
private float angle;
private float curAngle = 0;
private float rotateSpeed = 1;
void Start()
{
}
// Update is called once per frame
void Update()
{
//this.transform.LookAt(target.transform);
float dx = target.transform.position.x - this.transform.position.x;
float dz = target.transform.position.z - this.transform.position.z;
float hudu = Mathf.Atan2(dx, dz);
angle = hudu * 180 / Mathf.PI;
print(angle);
if (curAngle > 0 && angle < 0)
{
curAngle = angle;
}
else if (curAngle < 0 && angle > 0)
{
curAngle = angle;
}
if (curAngle < angle)
{
curAngle += rotateSpeed;
if (curAngle >= angle)
{
curAngle = angle;
}
}
else if (curAngle > angle)
{
curAngle -= rotateSpeed;
if (curAngle <= angle)
{
curAngle = angle;
}
}
this.transform.localEulerAngles = new Vector3(0, curAngle, 0);
}
}
opt子弹脚本:
using UnityEngine;
using System.Collections;
public class zidanfei : MonoBehaviour
{
private int flag = 0;
private static GameObject Baozha = null;
// Use this for initialization
void Start()
{
if (Baozha==null)
{
Baozha = GameObject.Find("baozha");
}
}
// 子弹飞
void Update()
{
if (flag == 0)
{
this.transform.Translate(Vector3.up * Time.deltaTime * 20);
}
}
void OnCollisionEnter(Collision colision)
{
flag = 1;
GameObject baozha = (GameObject)GameObject.Instantiate(Baozha);
baozha.transform.position = this.transform.position;
Destroy(baozha, 0.5f);
Destroy(gameObject);
}
//IEnumerator OnCollisionStay(Collision colisionInfo)
//{
// flag = 1;
// yield return new WaitForSeconds(1);
// Destroy(gameObject);
//}
}