坦克的移动和旋转

using UnityEngine;
using System.Collections;

public class Tank : MonoBehaviour {

	//子弹预设体
	public GameObject bullet;
	//发射点
	private Transform firePoint;
	//移动速度
	public float moveSpeed = 3f;
	//转身速度
	public float turnSpeed = 3f;
	//横纵轴
	float hor;
	float ver;

	void Start()
	{
		//获取发射点
		firePoint = transform.Find ("Top/Gun/FirePoint");
	}
	void Update()
	{
		//获取键盘纵轴值
		ver = Input.GetAxis ("Vertical");
		//坦克前后移动
		transform.position += transform.right * ver * Time.deltaTime * moveSpeed; 
		//获取键盘横轴值
		hor = Input.GetAxis ("Horizontal");
		//坦克转身
		transform.eulerAngles += hor * Vector3.up * turnSpeed;
		//如果按下空格发射子弹
		if (Input.GetKeyDown (KeyCode.Space)) {
			Fire ();
		}
	}

	/// <summary>
	/// 子弹发射
	/// </summary>
	void Fire()
	{
		//生成子弹
		GameObject currentBullet =(GameObject)Instantiate (bullet,firePoint.position, Quaternion.identity);
		//给子弹一个飞行方向
		currentBullet.GetComponent<Bullet> ().dir = transform.right;
	}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值