实现子弹, 可以指定发射位置, 发发射角度, 有方向, 可以反弹

实现子弹, 可以指定发射位置, 发发射角度, 有方向, 可以反弹,如图:


void CreateObstacleCopy::flyAction()
{
	int currentPositionX = getPositionX();//当前坐标
	int currentPositionY = getPositionY();
	speed = CCRANDOM_0_1() *500+200;//随机速度

	
	Point destinationPosition;
	int tempY = directionY;
	
	if (directionY >0)//向上运动
	{
			float areaHeight = visibleSize.height-currentPositionY;//获取飞行区域高度
			float getRandom = CCRANDOM_0_1() *(visibleSize.width+areaHeight*2);//随机左上右的一个位置
			
			if(getRandom >areaHeight+visibleSize.width)//飞行最后触碰到左边的位置
			{
				destinationPosition = Point( 0 , visibleSize.height-(getRandom - visibleSize.width-areaHeight));
				directionY=1;
			}
			else if(getRandom>areaHeight)//飞行最后触碰到上边的位置
			{
				destinationPosition = Point( getRandom-areaHeight , visibleSize.height);
				directionY = -1;
			}
			else//飞行最后触碰到右边的位置
			{
				destinationPosition = Point(visibleSize.width,  visibleSize.height-getRandom);
				directionY = 1;
			}
			//log("up");

	}
	else//向下运动
	{
		float areaHeight = currentPositionY;
		float getRandom = CCRANDOM_0_1() *(visibleSize.width+areaHeight*2);//随机左下右的一个位置

		if(getRandom >areaHeight+visibleSize.width)//飞行最后触碰到左边的位置
		{
			destinationPosition = Point( 0 , getRandom - visibleSize.width-areaHeight);
			directionY= -1;
		}
		else if(getRandom>areaHeight)//飞行最后触碰到下边的位置
		{
			destinationPosition = Point( getRandom-areaHeight , 0);
			directionY = 1;
		}
		else//飞行最后触碰到右边的位置
		{
			destinationPosition = Point(visibleSize.width, getRandom);
			directionY =-1;
		}
		//log("down");
	}

	float width = destinationPosition.x- currentPositionX;
	float heigth = destinationPosition.y - currentPositionY;//得到当前对象到目的地的矢量高
	
	float lenght = sqrt(width*width+heigth*heigth);//长度
	float radian = atan(width/ heigth);//h弧度
	float angle = (radian*360)/(2*3.141592);//化成角度了
	if(tempY<0) angle=180+angle;
	float during = lenght / speed;//飞行时间
	FiniteTimeAction* actionMoveTo = MoveTo::create(during,destinationPosition);//移动动画
	CallFunc * funcall= CallFunc::create(this, callfunc_selector(CreateObstacleCopy::animationDone));
	FiniteTimeAction* actionRotateTo = RotateTo::create(0.2,angle);//旋转动画
	FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);
	runAction(task);
}

void CreateObstacleCopy::animationDone()//动画结束后调用该函数
{
	//isCreate = false;
	flyAction();
}

void CreateObstacleCopy::firstLaunch()
{
	
	Point destinationPosition;
	
	//左上角到对象的角度
	float radian = atan(-getPositionX()/ (visibleSize.height-getPositionY()));//h弧度
	float angleUpLeft = (radian*360)/(2*3.141592);//化成角度了

	radian = atan((visibleSize.width-getPositionX())/ (visibleSize.height-getPositionY()));//h弧度
	float angleUpRight = (radian*360)/(2*3.141592);//化成角度了

	radian = atan( (getPositionY())/(visibleSize.width-getPositionX()));//h弧度
	float angleDownRight = (radian*360)/(2*3.141592)+90;//化成角度了

	 radian = atan(float(getPositionY())/ float(getPositionX()));//h弧度
	float angleDownLeft = -(radian*360)/(2*3.141592)-90;//化成角度了
	
	int angle = launchAngle;//传进来的方向
	if (angle>360+angleDownLeft)//角度转换,因为这的角度有负数,传进来的角度是正数
	{
		angle = angle - 360;
	}
	
	if (angleDownLeft<=angle &&angle<angleUpLeft)
	{

		float width =  getPositionX();
		float heigth = width/tan((launchAngle) *2*3.1415/360);//长不变, 算高

		destinationPosition.x = 0;
		destinationPosition.y = getPositionY()-heigth;//得到目的地位置
	

		float lenght = sqrt(width*width+heigth*heigth);

		speed = CCRANDOM_0_1() *200+500;
		float during = lenght / speed;//动画时间

		FiniteTimeAction* actionMoveTo = MoveTo::create(during,destinationPosition);//创建移动动画
		CallFunc * funcall= CallFunc::create(this, callfunc_selector(CreateObstacleCopy::animationDone));
		FiniteTimeAction* actionRotateTo = RotateTo::create(0.2,launchAngle);//旋转动画
		FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);
		runAction(task);
	}
  	else if(angleUpLeft<=angle &&angle<angleUpRight)
	{
		float heigth = visibleSize.height - getPositionY();
		float width = heigth*tan(launchAngle *2*3.1415/360);

		destinationPosition.x = getPositionX()+width;
		destinationPosition.y = visibleSize.height;
		directionY = -1;

		float lenght = sqrt(width*width+heigth*heigth);

		speed = CCRANDOM_0_1() *200+500;
		float during = lenght / speed;

		FiniteTimeAction* actionMoveTo = MoveTo::create(during,destinationPosition);
		CallFunc * funcall= CallFunc::create(this, callfunc_selector(CreateObstacleCopy::animationDone));
		FiniteTimeAction* actionRotateTo = RotateTo::create(0.2,launchAngle);
		FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);
		runAction(task);

	}

	else if(angleUpRight<=angle &&angle<angleDownRight)
	{

		float width = visibleSize.width - getPositionX();
		float heigth = width*tan((launchAngle-90) *2*3.1415/360);

		destinationPosition.x = visibleSize.width;
		destinationPosition.y = getPositionY()-heigth;


		float lenght = sqrt(width*width+heigth*heigth);

		speed = CCRANDOM_0_1() *200+500;
		float during = lenght / speed;

		FiniteTimeAction* actionMoveTo = MoveTo::create(during,destinationPosition);
		CallFunc * funcall= CallFunc::create(this, callfunc_selector(CreateObstacleCopy::animationDone));
		FiniteTimeAction* actionRotateTo = RotateTo::create(0.2,launchAngle);
		FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);
		runAction(task);
	}
	else
	{
		float heigth = getPositionY();
		float width = heigth*tan((launchAngle-180) *2*3.1415/360);

		destinationPosition.x = getPositionX()-width;
		destinationPosition.y = 0;
	

		float lenght = sqrt(width*width+heigth*heigth);
			directionY = 1;
		speed = 500;
		float during = lenght / speed;

		FiniteTimeAction* actionMoveTo = MoveTo::create(during,destinationPosition);
		CallFunc * funcall= CallFunc::create(this, callfunc_selector(CreateObstacleCopy::animationDone));
		FiniteTimeAction* actionRotateTo = RotateTo::create(0.2,launchAngle);
		FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);
		runAction(task);
	}
	
}


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Unity中实现子弹发射可以通过以下步骤: 1. 创建一个子弹预制件(Prefab),包含子弹模型和相关的脚本组件。 2. 在玩家角色或其他需要发射子弹的游戏对象上添加一个脚本,来控制子弹发射。 3. 在该脚本中,使用Instantiate()方法来实例化子弹预制件,并设置其位置和朝向。 4. 给子弹添加一个刚体组件,使其具有物理属性,例如重力和速度。 5. 在子弹脚本中使用AddForce()方法给子弹添加一个力,使其向前飞行。 下面是一个简单的示例代码,可以让玩家角色在按下射击键时发射子弹: ```csharp public class Shoot : MonoBehaviour { public GameObject bulletPrefab; // 子弹预制件 public Transform bulletSpawn; // 子弹发射点 public float bulletSpeed = 10f; // 子弹速度 void Update() { if (Input.GetButtonDown("Fire1")) { // 实例化子弹 GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation); // 添加刚体组件 Rigidbody rb = bullet.AddComponent<Rigidbody>(); // 添加向前的力 rb.AddForce(transform.forward * bulletSpeed, ForceMode.Impulse); } } } ``` 在这个例子中,我们在Update()方法中检测玩家是否按下了射击键(这里使用了Fire1虚拟按键,通常对应鼠标左键或左CTRL键)。如果按下了,我们就实例化子弹预制件,并在其位置和朝向上设置子弹的初始状态。接着,我们给子弹添加一个刚体组件,并使用AddForce()方法为其添加一个向前的力。这样,子弹就可以向前飞行了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值