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);
相关推荐
长脖鹿Johnny3 小时前
游戏输入系统框架设计(三):网络输入权威与可验证性
网络·游戏·游戏开发·架构设计·输入系统·网络同步
点心的游戏开发世界3 小时前
GDScript 入门笔记(二):变量与数据类型
笔记·游戏引擎·godot
一梭键盘任平生4 小时前
Shader入门笔记2
unity
谈资一本本4 小时前
电脑全能修复大师 Gilisoft Total Repair,智能检测修复系统、应用、游戏问题!DLL、DirectX、.NET运行库损坏修复+缺失自动补齐!
游戏·电脑·.net
点心的游戏开发世界12 小时前
Unity C# 脚本学习笔记:命名空间与 using
学习·unity·c#
程序员-Benothing12 小时前
H3 Max开放世界RPG:AI原生游戏,从“能生成“到“能玩“差了多远
游戏·ai-native
点心的游戏开发世界14 小时前
Unity C# 脚本学习笔记:泛型
学习·unity·c#
CoderYanger1 天前
A.每日一题:1140. 石子游戏 II
java·程序人生·算法·leetcode·游戏·职场和发展·深度优先
无别0521 天前
【做游戏】做完游戏Demo后没地方发布?
游戏·玩游戏
lilian2331 天前
HarmonyOS 7 新特性(二十九)|游戏伴随服务:悬浮协作与质量降级
游戏·语音识别·harmonyos