unity3d添加武器功能

本文主要介绍如何在Unity3D中利用Instantiate函数添加武器功能。讲解了Instantiate函数的作用,即复制一个Object,并提供了如何设置复制后的对象位置和旋转角。还特别指出,当设置对象速度方向时,不能直接使用(0,0,1)作为Z轴向量,应使用对象自身的forward属性来确保正确指向。
摘要由CSDN通过智能技术生成
using UnityEngine;
using System.Collections;

public class TankWeapons : MonoBehaviour {

	public GameObject shell;
	public float shootPower;//确定子弹的发射力
	public Transform shootPoint;//确定子弹的发射点。新建一个tank的空子物体,用来记录子弹的发射位置


	void Update () {

		if (Input.GetKeyDown (KeyCode.Space)) {//当按下空格时,调用Shoot()函数,所以用GetKeyDown
		
			Shoot();

		}
	
	}

	void Shoot(){

		GameObject newShell = Instantiate (shell, shootPoint.position, shootPoint.rotation) as GameObject;//用Instantiate函数克隆一个Object,并将Object转化为GameObject,存于newShell
		Rigidbody r = newShell.GetComponent<Rigidbody> ();//r定义为newShell的刚体
		r.velocity = shootPoint.forward * shootPower;//定义r的速度的方向及大小,速度方向为shootPoint.forward,大小为shootPower


	}

}


注:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值