Unity基础(六)小案例

1.通过向量和四元数控制子弹发射

子弹类

复制代码
public class bullet : MonoBehaviour
{
    public float speed;
    void Start()
    {
        Destroy(gameObject,3);
    }
    void Update()
    {
    this.transform.Translate(Vector3.forward * Time.deltaTime * speed);
    }
}

Vector3.forward使用的是常量,不出错。

发射控制

复制代码
enum E_BulletType
{
    One,
    Two,
    Three,
    Round
}
public class shoot : MonoBehaviour
{
    public GameObject bullet;
    private E_BulletType btype = E_BulletType.One;
    public int num = 4;
    // Start is called before the first frame update
    void Start()
    {
       
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
            {
            btype=E_BulletType.One;
            }
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            btype = E_BulletType.Two;
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            btype = E_BulletType.Three;
        }
        else if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            btype = E_BulletType.Round;
        }


        if (Input.GetKeyDown("a"))
        {
            switch (btype)
            {
                case E_BulletType.One:

                    GameObject.Instantiate(bullet, this.transform.position, this.transform.rotation);

                    break;
                case E_BulletType.Two:

                    GameObject.Instantiate(bullet, this.transform.position + this.transform.right*0.5f, this.transform.rotation);
                    GameObject.Instantiate(bullet, this.transform.position - this.transform.right * 0.5f, this.transform.rotation);
                    break;
                case E_BulletType.Three:
                    GameObject.Instantiate(bullet, this.transform.position, this.transform.rotation*(Quaternion.AngleAxis(20,Vector3.up)));
                    GameObject.Instantiate(bullet, this.transform.position, this.transform.rotation * (Quaternion.AngleAxis(-20, Vector3.up)));
                    GameObject.Instantiate(bullet, this.transform.position, this.transform.rotation);
                    break;
                case E_BulletType.Round:
                    float angle = 360 / num;
                    for (int i = 0; i <num; i++)
                    {

         GameObject.Instantiate(bullet, this.transform.position, this.transform.rotation*Quaternion.AngleAxis(i*angle,Vector3.up));
                    }                    
                    break;
            }
        }        
    }
}

要用Quaternion.AngleAxis(20,Vector3.up) ,也有Quaternion.AxisAngle 老方法(不使用)。

2.摄像机跟随效果

cs 复制代码
 //目标对象
 public Transform target;
 //相对头顶的偏移位置
 public float headOffsetH = 1;
 //倾斜角度
 public float angle = 45;
 //默认的 摄像机离观测点的距离
 public float dis = 5;

 //距离必须是3和10之间
 public float minDis = 3;
 public float maxDis = 10;
 //鼠标中间滚动控制的移动速度
 public float roundSpeed = 1;
 //看向对象时 四元数 旋转的速度
 public float lookAtSpeed = 2;
 //跟随对象移动的 速度
 public float moveSpeed = 2;
 //当前摄像机应该在的位置
 Vector3 nowPos;

 private Vector3 nowDir;

 //实现了鼠标中键 滚动 来改变摄像机远近
 dis += Input.GetAxis("Mouse ScrollWheel")*roundSpeed;
 dis = Mathf.Clamp(dis, minDis, maxDis);

 //向头顶偏移位置
 nowPos = target.position + target.up * headOffsetH;
 //往后方偏移位置
 nowDir = Quaternion.AngleAxis(angle, target.right) * -target.forward;
 nowPos = nowPos + nowDir * dis;

 //直接把算出来的位置 进行赋值
 //this.transform.position = nowPos;
 this.transform.position = Vector3.Lerp(this.transform.position, nowPos, Time.deltaTime* moveSpeed);

 Debug.DrawLine(this.transform.position, target.position + target.up * headOffsetH);
 //这里是 通过插值运算 来缓动 看向 物体
 this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(-nowDir), Time.deltaTime* lookAtSpeed);
相关推荐
め.1 天前
定点数运算库
算法·unity
天糊土1 天前
CentOS 7.6 YUM源更换完整教程
游戏
Duo1J1 天前
【UE】Slate 编辑器工具开发03 - 节点编辑器 (EdGraph)
ue5·编辑器·游戏引擎·ue4
郝学胜-神的一滴1 天前
[简化版 GAMES 104] 现代游戏引擎 02:拆解现代游戏引擎5+1层级架构,吃透引擎底层核心逻辑
c++·unity·架构·游戏引擎·图形渲染·unreal engine·系统设计
德迅云安全-上官1 天前
德迅云安全游戏盾与DDoS高防IP对比:差异、优势及选型指南
tcp/ip·游戏·ddos
LONGZETECH1 天前
新能源汽车充电设备装配与调试仿真教学软件 技术架构与核心实现解析
大数据·算法·3d·unity·架构·汽车
丁小未1 天前
Unity 几种常见合批手段的要求
游戏·unity·合批·srpbatcher·动态合批·静态合批
河南花仙子科技2 天前
花仙子科技详解:小游戏合规开发完整要点
科技·游戏
旧物有情2 天前
游戏开发常用架构 #MVP,MVC
游戏·unity·架构·mvc
笨鸟先飞的橘猫2 天前
游戏后端分布式学习——一致性协议Raft
分布式·学习·游戏