Project-怪物射手项目

目录

Project-怪物射手项目

实现功能图:

一、玩家功能实现

I、玩家移动,旋转

1、纹理贴图和法线贴图

2、虚拟轴实现人物的移动旋转

II、第一人称摄像机(口含摄像机:实现人物朝向人物正前方移动)


Project-怪物射手项目

实现功能图:

一、玩家功能实现

I、玩家移动,旋转

1、纹理贴图和法线贴图

纹理贴图:

法线贴图:

2、虚拟轴实现人物的移动旋转

(1)概述:通俗来讲,就是我们自己想象的一条范围在-1—1之间的数轴。

(2)作用:可以更加简便的获取键盘的输入,按下A键就会返回-1—0之间的值,按下D键就会返回0—1之间的值。

(3)设置虚拟轴

(4)使用虚拟轴实现人物的移动,旋转

public class Player : MonoBehaviour
{
    float speed = 5;
    // Start is called before the first frame update
    void Start()
    {
        
    }
​
    // Update is called once per frame
    void Update()
    {
        Move();
        Rotate();
    }
    
    void Move() 
    {
        float horzontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        transform.position += new Vector3(horzontal, 0, vertical)*speed*Time.deltaTime; 
    }
    
    //旋转
    void Rotate()
    {
        float mouseX = Input.GetAxis("Mouse X");
        transform.eulerAngles += Vector3.up * mouseX * speed;
    }
}

II、第一人称摄像机(口含摄像机:实现人物朝向人物正前方移动)

public class Player : MonoBehaviour
{
    float speed = 5;
    // Start is called before the first frame update
    void Start()
    {
       
    }
​
    // Update is called once per frame
    void Update()
    {
        Move();
        Rotate();
    }
​
     void Move() 
    {
        float horzontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        //transform.position += new Vector3(horzontal, 0, vertical)*speed*Time.deltaTime;
​
        Vector3 dir = new Vector3(horzontal, 0, vertical);
        dir = transform.TransformDirection(dir);//把世界坐标系转化为玩家坐标系
        transform.position += dir.normalized * speed * Time.deltaTime;
    
    }
​
    void Rotate()
    {
        float mouseX = Input.GetAxis("Mouse X");
        transform.eulerAngles += Vector3.up * mouseX * speed;
    }
}

​​​III、第三人称摄像机:(实现摄像机像自拍杆效果)

利用四元数实现摄像机的旋转: 理解为:Vector.up围绕dir(Vector3.right)顺时针旋转了90度(如果时-90就是逆时针旋转90度)

四元数:(来自百度百科)

 

实现原理:

 完整代码:

public class ThreeCamera : MonoBehaviour
{
    public Transform target;
    Vector3 offset = new Vector3();
    // Start is called before the first frame update
    private void OnEnable()
    {
        offset = transform.position - target.position;
    }
    void Start()
    {
        //Vector3 dir = Vector3.right*90;
        //Quaternion quaternion = Quaternion.Euler(dir);
        //Vector _dir = quaternion*Vector.up;//理解为:Vector.up围绕dir(Vector3.right)顺时针旋转了90度(如果时-90就是逆时针旋转90度)
 
    }
    // Update is called once per frame
    void Update()
    {
        transform.position = target.position + target.rotation * offset;
        transform.LookAt(target);
    }
}

IV、预制体,对象池,计时器(实现玩家子弹发射效果)

猜你喜欢

转载自blog.csdn.net/qq_53663718/article/details/127264084