Summary of unity development knowledge points 04

hybrid animation

Create a new blend tree in the animator controller, that is, create a blend animation
insert image description here
and then enter the blend animation, select the blend type as 1D (meaning that there is only one transfer parameter), and add two animations to this blend state, and set the blend state parameters Why is it worth enabling the corresponding animation at any time? The example I use here is to use 0 to represent walking, 1 to represent running, and an intermediate value to represent the superposition of the two states

insert image description here

IK realizes the rotation of a certain part of the body in a fixed direction

Determining the position of the head, hands, etc.

First of all, we need to set the created target object, that is, the object that the character needs to look at, as the target object of the character

insert image description here
At the same time, we need to enable the IK processing of the layer in the animator setter.
insert image description here
The next step is to write scripts for the characters to realize the movement of the characters, and at the same time, the head of the characters can be moved to a fixed position, or look at a fixed position.

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

public class peopleRun : MonoBehaviour
{
    
    
    private Animator animator;
    public Transform target;
    void Start()
    {
    
    
        animator = GetComponent<Animator>();
        
    }

    
    void Update()
    {
    
    
        //获取水平轴
        float horizontal = Input.GetAxis("Horizontal");
        //获取垂直轴
        float veritial = Input.GetAxis("Vertical");
        //创建方向向量
        Vector3 dir = new Vector3(horizontal,0,veritial);
        //当用户按下方向按键
        if (dir != Vector3.zero)
        {
    
    
            //更改人物朝向
            transform.rotation = Quaternion.LookRotation(dir);
            //播放移动动画
            animator.SetBool("run", true);
            //实现人物移动
            transform.Translate(Vector3.forward * 2 * Time.deltaTime);
        }
        else {
    
    
            //松开方向键恢复静止动画
            animator.SetBool("run",false);
        }
    }
    //实现人物部位指向固定位置
    private void OnAnimatorIK(int layerIndex)
    {
    
    
        //设置头部IK
        animator.SetLookAtWeight(1) ;
        //让角色实现头部旋转
        animator.SetLookAtPosition(target.position);
        //设置右手Ik权重
        animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
        //旋转权重
        animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);
        //设置手臂指向方向
        animator.SetIKPosition(AvatarIKGoal.RightHand,target.position);
        //设置手臂旋转方向
        animator.SetIKRotation(AvatarIKGoal.RightHand,target.rotation);
    }
}

The object moves to realize automatic navigation, and independently selects the operable route

First, we create a map with obstacles, and bake the map, and observe the operating range of the map area. Set
all obstacles to Navigation Static
insert image description here
and enter the navigation to bake the map.
insert image description here
The map will display the area where the player can walk.
insert image description here
Create a player entity. And add the Nav Mesh Agent component for the player, and write the navigation movement script for the object

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

public class NavigationTest : MonoBehaviour
{
    
    
    private NavMeshAgent agent;
    void Start()
    {
    
    
        //获取代理组件
        agent = GetComponent<NavMeshAgent>();
    }
    void Update()
    {
    
    
        //如果按下鼠标
        if (Input.GetMouseButtonDown(0)) {
    
    
            //获取点击位置
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            //判断射线是否碰到物体
            if (Physics.Raycast(ray,out hit)) {
    
     
                //点击的位置
                Vector3 point = hit.point;
                //设置该位置为导航目标点
                agent.SetDestination(point);    
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_48627750/article/details/129398034