第一人称的视角控制,移动,跳跃
原视频
https://www.bilibili.com/video/BV16t4y127fH?from=search&seid=13436451293195667046
测试场景
https://assetstore.unity.com/packages/3d/environments/landscapes/low-poly-simple-nature-pack-162153
下载好资源包,导入后打开第一个场景
导入后新建一个3D-胶囊体,命名为Player
,将相机保留至一个并命名为Main Camera
,然后新建空对象命名为GroundCheck
新建CameraController
和PlayerController
两个脚本,并分别与MainCamera
和Player
建立关联
CamereController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
public Transform player;
private float mouseX, mouseY; //获取鼠标移动的值
public float mouseSensitivity; //鼠标灵敏度
public float xRotation;
private void Start () {
Cursor.lockState = CursorLockMode.Locked;
}
private void Update () {
mouseX = Input.GetAxis ("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = Input.GetAxis ("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp (xRotation, -70f, 70f);
player.Rotate (Vector3.up * mouseX);
transform.localRotation = Quaternion.Euler (xRotation, 0, 0);
}
}
PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private CharacterController cc;
public float moveSpeed;
public float jumpSpeed;
private float horizontalMove, verticalMove;
private Vector3 dir;
public float gravity;
private Vector3 velocity;
public Transform groundCheck;
public float checkRadius;
public LayerMask groundLayer;
public bool isGround;
private void Start () {
cc = GetComponent<CharacterController> ();
}
private void Update () {
isGround = Physics.CheckSphere (groundCheck.position, checkRadius, groundLayer);
if (isGround && velocity.y < 0) {
velocity.y = -2f;
}
horizontalMove = Input.GetAxis ("Horizontal") * moveSpeed;
verticalMove = Input.GetAxis ("Vertical") * moveSpeed;
dir = transform.forward * verticalMove + transform.right * horizontalMove;
cc.Move (dir * Time.deltaTime);
if (Input.GetButtonDown ("Jump") && isGround) {
velocity.y = jumpSpeed;
}
velocity.y -= gravity * Time.deltaTime;
cc.Move (velocity * Time.deltaTime);
}
}
为场景添加一个Ground
图层
将场景改为Ground
图层
修改脚本属性值
如果没有碰撞把fbx模型的此项勾选上
效果
装备自旋转与拾取
https://blendswap.com/blend/26019#
使用blender导出fbx,注意坐标系
导入时勾选
导入场景摆好位置
给gun
添加Equipment
的Tag与Layer
给Player
添加Player
的Layer
项目设置中Player
和Equipment
图层取消勾选,忽略碰撞
新建Equipment.cs
,与gun
绑定
Equipment.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Equipment : MonoBehaviour {
public float speed = 90f;
public float moveSpeed = 2f;
void Update () {
transform.Rotate (Vector3.forward * Time.deltaTime * speed);
}
}
CameraController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
public float pickDistance;
private void Start () {
pickDistance = 2f;
}
private void Update () {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
if (hit.transform.tag == "Equipment") {
if (hit.distance <= pickDistance) {
if (Input.GetKeyDown (KeyCode.E)) {
DestroyImmediate(hit.transform.gameObject);
}
}
}
}
}
}
效果:靠近物品时按E会“收取”物品
人物的行走动画
https://assetstore.unity.com/packages/3d/props/polygon-starter-pack-156819
将前面的主角替换为素材包内人物
找到模型给它添加动画
因为这个素材自带动画,所以稍微剪辑一下,生成一个跑步时的动画和一个静止时的动画
修改Rig
属性
给Player
添加Animation
组件
修改Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private CharacterController cc;
private void Start () {
ani=GetComponent<Animation>();
}
private void Update () {
if(Input.GetKey(KeyCode.W)){
ani.Play("Run");
}
if(Input.GetKeyUp(KeyCode.W)){
ani.Play("Stay");
}
}
}
效果
2D按钮的拖动
随便来张卡牌和背景
将卡牌当做按钮
可以方便地添加按下时的效果
编写脚本Card.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Card : MonoBehaviour {
// Start is called before the first frame update
private Vector3 lastMousePosition = Vector3.zero;
private Vector3 originPosition;
private bool isDrag = false;
void Start () {
}
// Update is called once per frame
void Update () {
}
public void MouseEnter () {
if (!isDrag) {
originPosition = transform.position;
}
}
public void MouseExit () {
}
public void MouseDrag () {
isDrag=true;
if (lastMousePosition != Vector3.zero&&Input.GetMouseButton(0)) {
Vector3 offset = Input.mousePosition - lastMousePosition;
transform.position += offset;
}
lastMousePosition = Input.mousePosition;
}
public void EndDrag () {
isDrag=false;
transform.position = originPosition;
lastMousePosition = Vector3.zero;
}
}
添加EventTigger
组件,绑定脚本里的函数
效果
技能CD效果
技能图片
新建两个Image
,一个Text
将Mask
的颜色改为偏灰色
新建一个脚本Skill.cs
,用来控制Skill
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Skill : MonoBehaviour {
public Image mask;
public Text cd;
public float cdTime = 5f;
public bool isCD = false;
private bool isClick = false;
void Start () {
}
void Update () {
if (isClick && !isCD) {
isCD = true;
mask.fillAmount = 1;
}
if (isCD) {
mask.fillAmount -= Time.deltaTime / cdTime;
cd.text = "" + Mathf.Ceil (mask.fillAmount * cdTime);
cdTime -= Time.deltaTime;
if (mask.fillAmount == 0) {
isCD = false;
cd.text = "";
cdTime = 5;
}
}
}
public void mouseDown () {
if (Input.GetMouseButtonDown (0)) {
isClick = true;
}
}
public void mouseUp () {
if (Input.GetMouseButtonUp (0)) {
isClick = false;
}
}
}
为Skill
添加Event Trigger
组件,并绑定Skill.cs
里的函数
Mask
和Cd
选择对应的对象
Mask
和Cd
的Raycast Target
取消勾选,否则Skill
不会响应点击
效果