Unity's most simple and practical protagonist mobile teaching

 The protagonist is using a capsule body at this time, add a capsule collision body and a character controller:

Added a groundcheck for the character to judge whether it is on the ground

 

Finished, finally attach the code: Just hang it on the character

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

public class PlayerMovement : MonoBehaviour
{
    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = -9.8f;
    public float jumpHeight = 3f;
    public Transform groundCheck;
    public float groundDistance = .4f;
    public LayerMask groundMask;

    private Vector3 velocity;
    private CharacterController controller;
    private bool isGrounded;

    // Start is called before the first frame update
    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
            if (isGrounded && velocity.y < 0)
            {
                velocity.y = -2f;
            }

        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector3 moveDirection = transform.right * horizontal + transform.forward * vertical;

        controller.Move(moveDirection * speed * Time.deltaTime);

            if (Input.GetButtonDown("Jump") && isGrounded)
            {
                velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
            }

        velocity.y += gravity * Time.deltaTime;
        
        controller.Move(velocity * Time.deltaTime);

    }

}

 

Next, there is another character script control (reproduced), which I also think is very good. Let me simply share and record it:

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

public class PlayerControaller : MonoBehaviour
{
    public GameObject myBag;
    bool isOpen;
    //获得Player的CharacterController组件
    private CharacterController cc;
    [Header("移动参数")]
    //定义player的移动速度
    public float moveSpeed;
    [Header("跳跃参数")]
    //定义player的跳跃速度
    public float jumpSpeed;
    public float HP = 100;

    //定义获得按键值的两个变量
    private float horizontalMove, verticalMove;

    //定义三位变量控制方向
    private Vector3 dir;
    //定义重力变量
    public float gravity;
    //定义y轴的加速度
    private Vector3 velocity;
    //检测点的中心位置
    public Transform groundCheck;
    //检测点的半径
    public float checkRadius;
    //定义需要检测的图层
    public LayerMask groundLayer;
    [Header("检测角色状态")]
    public bool isOnground;
    public bool isJump;
    private float attackTimer;
    public float attackTime;
    void Start()
    {
        attackTime = 4;
        attackTimer = attackTime;
        //用GetComponent<>()方法获得CharacterController
        cc = GetComponent<CharacterController>();
    }


    void Update()
    {
        Attacktimer();
        OpenMyBag();
        //使用Physics.CheckSphere()方法改变isOnground的值为true
        //isOnground = Physics.CheckSphere(groundCheck.position, checkRadius, groundLayer);

        //if (isOnground && velocity.y < 0)
        //{
        //    velocity.y = -1f;
        //}

        //用Input.GetAxis()方法获取按键左右移动的值
        horizontalMove = Input.GetAxis("Horizontal") * moveSpeed;
        //用Input.GetAxis()方法获取按键前后移动的值
        verticalMove = Input.GetAxis("Vertical") * moveSpeed;
        if(horizontalMove != 0 || verticalMove != 0)
        {

        }
        else
        {

        }

        //将方向信息存储在dir中
        dir = transform.forward * verticalMove + transform.right * horizontalMove;
        //用CharacterController中的Move()方法移动Player
        cc.Move(dir * Time.deltaTime);

        //当键盘按空格的时候可以完成角色的跳跃,并且使角色只能够跳跃一次
        if (Input.GetButtonDown("Jump") /*&& isOnground*/)
        {
            velocity.y = jumpSpeed;
            isJump = true;
        }

        //通过每秒减去重力的值不断下降
        velocity.y -= gravity * Time.deltaTime;
        //用CharacterController中的Move()方法移动y轴
        cc.Move(velocity * Time.deltaTime);
    }

    void OpenMyBag()
    {
        if (Input.GetKeyDown(KeyCode.B))
        {
            isOpen = !myBag.activeSelf;
            myBag.SetActive(isOpen);
        }
    }
    private void OnTriggerStay(Collider other)
    {
        if (other.tag == "Enemy" &&  attackTimer <= 0)
        {
            attackTimer = attackTime;
            Debug.Log("kill");
            HP -= 5;
            if (HP == 0)
            {
                gameObject.SetActive(false);
            }

        }
    }
    void Attacktimer()
    {
        if (attackTimer > 0)
        {
            attackTimer -= Time.deltaTime * 2;
        }

    }
}

Learn the hands-on code by yourself, adding functions such as opening the backpack and attacking intervals in the middle, pay attention to my follow-up explanation! 

 

 

Guess you like

Origin blog.csdn.net/weixin_51083610/article/details/126284458