Unity 飞机的子弹轨迹
最近公司在开发一款儿童打飞机游戏. 策划跟我说能在子弹上加上一些轨迹就好了. 比如 旋转 左右移动呀.然后它就很愉快的跑去截其他游戏的图啦。。。
我看见图的时候,

解决方案:
1. 使用牛逼的算法,实现子弹轨迹的移动(第一种应该是正确的解决方案)
2. 发射子弹次数 + 前后移动 + 左右移动 + 围绕某点旋转 + 自身旋转 = 子弹轨迹. 采用组合方式实现
目前采用第二种方式:
我们来看下子弹,两个齿轮会绕着中心旋转, 并且向下移动. ( 围绕某点旋转 + 自身旋转 + 前后移动 = 实现效果)

子弹的GameObject节点层次关系: (此结构和任意组合,但不要把全部功能放在同一个节点上. =。=这样理不清还会受到干扰)
前后移动节点(看两个齿轮中心点向下移动)
左右移动节点(无这个功能,无需开启脚本)
围绕父节点旋转节点(两个此轮围绕中心旋转)
自身旋转节点(比如齿轮它自身会旋转)

下面的子弹实现方式: 自身旋转 + 左右移动 + 上下移动 = 实现效果

============================================================================================================================
代码区域:
左右移动脚本:
using UnityEngine;
using System.Collections; namespace Bullet
{
/// <summary>
/// 子弹横向左右移动
/// </summary>
public class BulletHorizontal : MonoBehaviour
{
public bool isLeft; //是否向左移动
public float moveLocation; //移动的位置
public float speed; //移动速度
private Vector3 leftPosition; //左边的目标点
private Vector3 rightPosition; //右边的目标点 void Awake()
{
leftPosition = new Vector3(transform.localPosition.x - moveLocation, transform.localPosition.y, transform.localPosition.z);
rightPosition = new Vector3(transform.localPosition.x + moveLocation, transform.localPosition.y, transform.localPosition.z);
} void Update()
{ if (isLeft)
{
transform.localPosition += (Vector3.right * speed * Time.deltaTime);
//transform.Translate(Vector3.right * speed * Time.deltaTime); if (transform.localPosition.x >= rightPosition.x)
{
isLeft = !isLeft;
}
}
else
{
transform.localPosition += (Vector3.left * speed * Time.deltaTime);
//transform.Translate(Vector3.left * speed * Time.deltaTime); if (transform.localPosition.x <= leftPosition.x)
{
isLeft = !isLeft;
}
}
}
}
}
子弹旋转脚本:
using UnityEngine;
using System.Collections;
using BearGame; namespace Bullet
{
/// <summary>
/// 子弹旋转
/// </summary>
public class BulletRotate : MonoBehaviour
{
public RotationDirection rotationDirection;
public float speed;
public Transform rotateNode; public void Update()
{
if (rotateNode != null)
{
//围绕某一节点旋转
switch (rotationDirection)
{
case RotationDirection.Left:
this.transform.RotateAround(rotateNode.transform.position, Vector3.forward, speed);
break;
case RotationDirection.Right:
this.transform.RotateAround(rotateNode.transform.position, Vector3.forward, -(speed));
break;
}
}
else
{
//自身旋转
switch (rotationDirection)
{
case RotationDirection.Left:
this.transform.Rotate(Vector3.forward, speed);
break;
case RotationDirection.Right:
this.transform.Rotate(Vector3.forward, -(speed));
break;
}
}
}
} /// <summary>
/// 旋转的方向
/// </summary>
public enum RotationDirection
{
Left,
None,
Right
}
}
子弹前后移动:
using UnityEngine;
using System.Collections;
using BearGame; namespace Bullet
{
/// <summary>
/// 子弹前后移动
/// </summary>
public class BulletVertical : MonoBehaviour
{
public float speed = 0;
public BulletDirectionEnum direction;
private Vector3 dir; public delegate void BulletOutScreen(GameObject gameObject);
/// <summary>
/// 子弹越屏之后,触发的事件,用于销毁子弹
/// </summary>
/// <param name="gameObject"></param>
public static event BulletOutScreen OnBulletDestroy; void Start()
{
switch (direction)
{
case BulletDirectionEnum.Up:
dir = Vector3.up;
break;
case BulletDirectionEnum.Bottom:
dir = -Vector3.up;
break;
case BulletDirectionEnum.Left:
dir = Vector3.left;
break;
case BulletDirectionEnum.Right:
dir = Vector3.right;
break;
}
} void Update()
{
transform.Translate(dir * speed * Time.deltaTime);
if (transform.localPosition.y > Screen.height)
{
transform.gameObject.SetActive(false);
//调用子弹出屏幕事件
}
}
} /// <summary>
/// 子弹移动的方向
/// </summary>
public enum BulletDirectionEnum
{
Up,
Bottom,
Left,
Right,
None
}
}
子弹轨迹总体脚本控制:
using UnityEngine;
using System.Collections;
using BearGame; namespace Bullet
{
public class BulletMode : MonoBehaviour
{
/// <summary>
/// 子弹预设
/// </summary>
private GameObject bullet; /// <summary>
/// 子弹的名称
/// </summary>
private string bulletName; /// <summary>
/// 子弹爆炸特效
/// </summary>
public Transform bulletEffect; /// <summary>
/// 向左右移动
/// </summary>
public BulletHorizontal bulletHorizontal; /// <summary>
/// 向前后移动
/// </summary>
public BulletVertical bulletForce; /// <summary>
/// 围绕某一个点旋转
/// </summary>
public BulletRotate bulletRotateARound; /// <summary>
/// 围绕自身旋转
/// </summary>
public BulletRotate bulletRotateOneself; #region 属性
public GameObject Bullet
{
get
{
if (bullet == null)
{
bullet = this.gameObject;
}
return bullet;
}
set { bullet = value; }
}
public string BulletName
{
get
{
if (string.IsNullOrEmpty(bulletName))
{
bulletName = this.gameObject.name;
}
return bulletName;
}
set { bulletName = value; }
}
#endregion public void TrunOFFALLScript()
{
if (bulletRotateOneself != null) bulletRotateOneself.enabled = false;
if (bulletRotateARound != null) bulletRotateARound.enabled = false;
if (bulletForce != null) bulletForce.enabled = false;
if (bulletHorizontal != null) bulletHorizontal.enabled = false; } public void TrunOpenALLScript()
{
if (bulletRotateOneself != null) bulletRotateOneself.enabled = true;
if (bulletRotateARound != null) bulletRotateARound.enabled = true;
if (bulletForce != null) bulletForce.enabled = true;
if (bulletHorizontal != null) bulletHorizontal.enabled = true;
} } }
Unity 飞机的子弹轨迹的更多相关文章
- [知了堂学习笔记]_用JS制作《飞机大作战》游戏_第4讲(创建敌方飞机、敌方飞机发射子弹、玩家子弹击中敌方小飞机,小飞机死亡)
一.创建敌方飞机 1.思考创建思路: 创建敌方飞机思路与创建玩家飞机思路一样: (1)思考敌方飞机具备什么属性: 敌方飞机的图片.坐标.飞行速度.状态(是否被击中) 设置小飞机被击中时消失时间.飞机可 ...
- 用JS制作《飞机大作战》游戏_第4讲(创建敌方飞机、敌方飞机发射子弹、玩家子弹击中敌方小飞机,小飞机死亡)-陈远波
一.创建敌方飞机 1.思考创建思路: 创建敌方飞机思路与创建玩家飞机思路一样: (1)思考敌方飞机具备什么属性: 敌方飞机的图片.坐标.飞行速度.状态(是否被击中) 设置小飞机被击中时消失时间.飞机可 ...
- android小游戏 飞机躲子弹
最近android老师让每人写一个小东西,因为之前学awt时写过一个java版的飞机躲子弹,所以这次想写成android版的. 文件直接导入就行http://files.cnblogs.com/fil ...
- Egret学习笔记 (Egret打飞机-8.敌机和主角飞机发射子弹)
经过前面几章的准备,我们差不多已经具备了一个基本的框架,这一章我们就开始添砖加瓦了. 敌机定时发射一个子弹,子弹的方向是从上到下,但是发射子弹的代码应该放在哪儿呢? 从面向对象编程的思想来说,子弹是敌 ...
- day 5 飞机发射子弹 难点??
1.效果图 2.飞机发出子弹 #-*- coding:utf-8 -*- import pygame import time from pygame.locals import * class Her ...
- Egret学习笔记 (Egret打飞机-9.子弹对敌机和主角的碰撞)
运行起来,虽然主角飞机和敌机都在互相发射子弹,但是子弹打中了就和没打中效果是一样的.. 这一章我们就来处理子弹和飞机的碰撞问题. 我们所有的操作都是基于Main这个容器来做的.所以我就把这个处理放到M ...
- [知了堂学习笔记]_用JS制作《飞机大作战》游戏_第3讲(玩家发射子弹)
一.公布上一讲中玩家飞机上.下.右移动实现的代码: /*=========================键盘按下事件 keycode为得到键盘相应键对应的数字==================== ...
- 用JS制作《飞机大作战》游戏_第3讲(玩家发射子弹)-陈远波
一.公布上一讲中玩家飞机上.下.右移动实现的代码: /*=========================键盘按下事件 keycode为得到键盘相应键对应的数字==================== ...
- Egret学习笔记 (Egret打飞机-5.实现子弹对象)
上一章把飞机添加到屏幕上,但是飞机要发射子弹对吧?那么这一章我们就来实现一下发射子弹,并实现一个简单的子弹对象池 先来捋一捋思路 1.创建一个子弹对象 2.然后添加一个bitmap,显示子弹贴图 3. ...
随机推荐
- poj 3579 Median (二分搜索之查找第k大的值)
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...
- pyqt treeview基础学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from Py ...
- Hibernate框架后续
持久化对象的唯一标识OID 1:我们都知道,在java中按照内存地址来区分同一个类的不同对象 而关系数据库按照主键来区分一条记录 在Hibernate中使用OID来建立内存中的对象和数据 ...
- 多线程下不反复读取SQL Server 表的数据
在进行一些如发送短信.邮件的业务时,我们常常会使用一个表来存储待发送的数据,由后台多个线程不断的从表中读取待发送的数据进行发送.发送完毕后再将数据转移到历史表中,这样保证待发送表的数据普通情况下不会太 ...
- 泛泰A880S升级官方4.4.2 binx教程
之前为了尝鲜,直接官升4.4.2,红砖了.越南兄弟说官方没更新升级工具,所以导致升级到78%就停止了,他给的办法,我试也没成功.官方旧版本号又不能升级S0221118以上的版本号,新版的离线升级工具没 ...
- Timer.5 - Synchronising handlers in multithreaded programs
This tutorial demonstrates the use of the boost::asio::strand class to synchronise callback handlers ...
- Swift学习之类和结构体的创建
随着一步步的学习,学习到的新知识越来越多了,不管是新的还是旧的,都禁不住时间的堆积,再熟悉的知识点时间久了都会渐渐的忘记,也许这就是人们生活中一种潜在的惰性吧,看似非常熟悉的东西,等到真正要用的时候, ...
- 用cflow工具生成代码函数调用关系
1. 安装 sudo apt-get install cflow 2.使用 cflow [options...] [file]... 例: cflow main.c 生成main.c文件例的函数调用关 ...
- MySql命令——函数
1.拼接字段——Concata() 把多个串连接起来形成一个较长的串. select concat(value,'(',id,')') from test; 2.去掉空格 RTrim() 去掉右边的空 ...
- js判断ie浏览器
function isIE() { //ie? if (!!window.ActiveXObject || "ActiveXObject" in window){ document ...