Unity-personal development skills

Unity uses operation skills

Unity Camera Perspective

Personal recommendation, right mouse button and keyboard wasd key to control the viewing angle

The difference between public attributes and private attributes

    public CharacterController characterController;
    private CharacterController characterController;

public (not recommended)

You can directly drag and drop the panel to complete the attribute assignment (but not recommended, it is easy to lose attributes when packaging the project)
insert image description here

private (recommended)

insert image description here

Assets removed from the Assets folder can be restored

insert image description here

node binding

insert image description here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EmptyTest : MonoBehaviour
{
    
    
    public GameObject Cube;
    // Start is called before the first frame update
    void Start()
    {
    
    
        //立方体的名称
        Debug.Log(Cube.name);
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        
    }
}

Note layer overlays

The big one covers the small one, bg covers the role
insert image description here
insert image description here

How the background overlay works

Pay attention to the values ​​of two backgrounds, one player and Layer, the larger one covers the smaller one, and the lower one covers the upper one
insert image description here

game resolution settings

insert image description here

pakage Manager resource removal

insert image description here
It will display all purchased resources, if you don't want it to be displayed, you can only hide it in the resource store
insert image description here

project rename

insert image description here

character movement

character movement

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMoveController : MonoBehaviour
{
    
    
    private CharacterController characterController;
    public float speed = 10f;       //移动速度
    public Vector3 moveDirection;    //设置移动方向
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        //获取player身上的CharacterController组件
        characterController = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
    
    
        Move();
    }

    public void Move()
    {
    
    
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        moveDirection = (transform.right * horizontal + transform.forward * vertical).normalized;    //设置玩家移动方向

        characterController.Move(moveDirection * speed * Time.deltaTime);
    }
}



The camera angle follows the character

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotation : MonoBehaviour
{
    
    
    public float mouseSensitivity = 1000f;   //实现灵敏度
    public Transform playerBody;    //玩家位置
    public float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
    
    
    }

    // Update is called once per frame
    void Update()
    {
    
    
        // * Time.deltaTime是时间标准化,是每秒进行控制,而不是每帧进行控制
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
        xRotation -= mouseY;    //将上下旋转的轴值进行累加
        xRotation = Mathf.Clamp(xRotation, -80f, 80f); //限制轴值的累计(正负80)
        
        transform.localRotation = Quaternion.Euler(xRotation,0f,0f);
        playerBody.Rotate(Vector3.up * mouseX); //玩家横向旋转
    }

  
}

camera turn

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotation : MonoBehaviour
{
    
    
    public float mouseSensitivity = 1000f;   //实现灵敏度
    public Transform playerBody;    //玩家位置
    public float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
    
    
    }

    // Update is called once per frame
    void Update()
    {
    
    
        // * Time.deltaTime是时间标准化,是每秒进行控制,而不是每帧进行控制
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
        xRotation -= mouseY;    //将上下旋转的轴值进行累加
        xRotation = Mathf.Clamp(xRotation, -80f, 80f); //限制轴值的累计(正负80)
        
        transform.localRotation = Quaternion.Euler(xRotation,0f,0f);
        playerBody.Rotate(Vector3.up * mouseX); //玩家横向旋转
    }

  
}

align view

Let the game display the same effect as the Scene view
insert image description here
insert image description here
insert image description here

FPS game

character movement

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class PlayerMoveComment : MonoBehaviour
{
    
    
    private CharacterController characterController;
    public float speed = 10f;       //移动速度
    public Vector3 moveDirection;    //设置移动方向
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        //获取player身上的CharacterController组件
        characterController = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
    
    
        Move();
    }

    public void Move()
    {
    
    
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        moveDirection = (transform.right * horizontal + transform.forward * vertical).normalized;    //设置玩家移动方向

        characterController.Move(moveDirection * speed * Time.deltaTime);
    }
}

angle of view

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
///摄像机的旋转
/// 玩家左右旋转控制实现左右旋转
/// 摄像机上下旋转控制实现上下旋转
/// </summary>
public class MouseLook : MonoBehaviour
{
    
    
    public float mouseSensitivity = 1000f;   //实现灵敏度
    public Transform playerBody;    //玩家位置
    public float xRotation = 0f;

    private void Start()
    {
    
    
        //将光标锁定在该游戏窗口的中心,隐藏硬件光标
        Cursor.lockState  = CursorLockMode.Locked;
    }

    void Update()
    {
    
    
        // * Time.deltaTime是时间标准化,是每秒进行控制,而不是每帧进行控制
         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
         xRotation -= mouseY;    //将上下旋转的轴值进行累加
         xRotation = Mathf.Clamp(xRotation, -80f, 80f); //限制轴值的累计(正负80)
        
         transform.localRotation = Quaternion.Euler(xRotation,0f,0f);
         playerBody.Rotate(Vector3.up * mouseX); //玩家横向旋转
    }
}

mouse control camera rotation

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
///摄像机的旋转
/// 玩家左右旋转控制实现左右旋转
/// 摄像机上下旋转控制实现上下旋转
/// </summary>
public class MouseLook : MonoBehaviour
{
    
    
    public float mouseSensitivity = 1000f;   //实现灵敏度
    public Transform playerBody;    //玩家位置
    public float xRotation = 0f;

    private void Start()
    {
    
    
        //将光标锁定在该游戏窗口的中心,隐藏硬件光标
        Cursor.lockState  = CursorLockMode.Locked;
    }

    void Update()
    {
    
    
        // * Time.deltaTime是时间标准化,是每秒进行控制,而不是每帧进行控制
         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
         xRotation -= mouseY;    //将上下旋转的轴值进行累加
         xRotation = Mathf.Clamp(xRotation, -80f, 80f); //限制轴值的累计(正负80)
        
         transform.localRotation = Quaternion.Euler(xRotation,0f,0f);
         playerBody.Rotate(Vector3.up * mouseX); //玩家横向旋转
    }
}

RPG game

character movement

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class MaleController : MonoBehaviour
{
    
    
    //玩家面对的对象,IK
    public Transform target;
    //animator组件
    private Animator animator;
    // Start is called before the first frame update
    void Start()
    {
    
    
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
    
    
        //水平轴
        float horizontal = Input.GetAxis("Horizontal");
        //垂直轴
        float vertical = Input.GetAxis("Vertical");
        //向量
        Vector3 dir = new Vector3(horizontal,0,vertical);
        
        AnimatorStateInfo stateinfo = animator.GetCurrentAnimatorStateInfo(0);
        if (dir != Vector3.zero && !stateinfo.IsName("pickup") && !stateinfo.IsName("wave"))
        {
    
    
            //面向向量              quaternion是一个四元数的类,生成的四元数给旋转
            transform.rotation = Quaternion.LookRotation(dir);
            //播放跑步动画
            animator.SetBool("isRun",true);
            //使角色朝向前方移动
            transform.Translate(Vector3.forward * 2 * Time.deltaTime);
        }
        else
        {
    
    
            animator.SetBool("isRun",false);
        }
        //判断点击键盘F键时触发pickup
        if (Input.GetKeyDown(KeyCode.F))
        {
    
    
            animator.SetTrigger("pickup");
        }
        
        if (Input.GetKeyDown(KeyCode.G))
        {
    
    
            animator.SetTrigger("wave");
        }
    }
    
    //IK写道这个方法内
    private void OnAnimatorIK(int layerIndex)
    {
    
    
        //设置头部IK权重
        animator.SetLookAtWeight(1);
        //使头部看向这个位置
        animator.SetLookAtPosition(target.position);
        
        //设置右手IK权重
        // animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
        // //旋转权重
        // animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);
        // //设置右手IK
        // animator.SetIKPosition(AvatarIKGoal.RightHand,target.position);
        // animator.SetIKRotation(AvatarIKGoal.RightHand,target.rotation);
    }
}


collider trigger

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class PlayerColliderDesk : MonoBehaviour

{
    
    

    private VideoPlayer video;

    //监听发生碰撞
    // private void OnCollisionEnter(Collision collsion)
    // {
    
    
    //     Debug.Log(collsion.collider.name);
    //     GameObject Plane_Video = GameObject.Find("Plane_Video");
    //     video = Plane_Video.GetComponent<VideoPlayer>();
    //     if (video.isPlaying && collsion.collider.name.Equals("TV_furniture"))
    //     {
    
    
    //         Debug.Log("暂停");
    //         video.Pause();
    //     }
    // }
    //
    // //持续碰撞中
    // private void OnCollisionStay(Collision collsion){
    
    
    //
    //
    // }
    //
    // //碰撞结束
    // private void OnCollisionExit(Collision collsion)
    // {
    
    
    //     if (!video.isPlaying & collsion.collider.name.Equals("TV_furniture"))
    //     {
    
    
    //         Debug.Log("开始");
    //         video.Play();
    //     }
    // }
    

    //触发开始
    private void OnTriggerEnter(Collider other)
    {
    
    
        Debug.Log(other.name);
        Debug.Log("触发器执行");
        GameObject Plane_Video = GameObject.Find("Plane_Video");
        video = Plane_Video.GetComponent<VideoPlayer>();
        if (video.isPlaying && other.name.Equals("TV_furniture"))
        {
    
    
            Debug.Log("暂停");
            video.Pause();
        }
    }

    //正在触发
    private void OnTriggerStay(Collider other)
    {
    
    
    }

    //触发结束
    private void OnTriggerExit(Collider other)
    {
    
    
        if (!video.isPlaying && other.name.Equals("TV_furniture"))
        {
    
    
            Debug.Log("开始");
            // video.Play();
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_39123467/article/details/128511399