主角此时使用的是胶囊体,增加胶囊碰撞体和角色控制器:
为角色新增groundcheck判断是否落地
完成,最后附上代码:挂到角色身上即可
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);
}
}
接下来还有另外一种角色脚本控制(转载)我也觉得特别不错,简单分享记录一下:
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;
}
}
}
自己学习动手的代码,中间加入了打开背包和攻击间隔等功能,关注我后续讲解!