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);
相关推荐
无相孤君3 小时前
我用 Docker + JunimoServer 搭了一个星露谷物语无头服,还顺手做了个本地管理面板
linux·游戏·docker·开源
xifangge20253 小时前
Steam/Epic 游戏启动报错 0xc000007b / msvcp140.dll 缺失?VC++ 运行库底层修复指南
开发语言·c++·游戏
a58808114 小时前
星际争霸1原版安装包——游戏玩法、配置要求与详细安装教程
windows·游戏·游戏程序
littleschemer4 小时前
Go:实现游戏服务器网关
服务器·网关·游戏·golang
Sator16 小时前
Unity2022版接入MCP
unity·ai编程
魔法阵维护师6 小时前
从零开发游戏需要学习的c#模块,第二十三章(粒子效果 —— 让游戏“活”起来本课目标)
学习·游戏·c#
魔法阵维护师6 小时前
从零开发游戏需要学习的c#模块,第二十二章(音效与背景音乐)
学习·游戏·c#
wanhengidc16 小时前
私有云的作用都有哪些?
运维·服务器·网络·游戏·智能手机
魔法阵维护师17 小时前
从零开发游戏需要学习的c#模块,第十六章(安装 MonoGame 并创建第一个窗口)
学习·游戏·c#·monogame