unity跳跃按钮_Unity笔记(09200924)

第一人称的视角控制,移动,跳跃

原视频

https://www.bilibili.com/video/BV16t4y127fH?from=search&seid=13436451293195667046

测试场景

https://assetstore.unity.com/packages/3d/environments/landscapes/low-poly-simple-nature-pack-162153

unity跳跃按钮_Unity笔记(09200924)_第1张图片

下载好资源包,导入后打开第一个场景

unity跳跃按钮_Unity笔记(09200924)_第2张图片

导入后新建一个3D-胶囊体,命名为Player,将相机保留至一个并命名为Main Camera,然后新建空对象命名为GroundCheck

unity跳跃按钮_Unity笔记(09200924)_第3张图片

新建CameraControllerPlayerController两个脚本,并分别与MainCameraPlayer建立关联

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 ();    }    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图层

unity跳跃按钮_Unity笔记(09200924)_第4张图片

将场景改为Ground图层

unity跳跃按钮_Unity笔记(09200924)_第5张图片

修改脚本属性值

unity跳跃按钮_Unity笔记(09200924)_第6张图片

unity跳跃按钮_Unity笔记(09200924)_第7张图片

如果没有碰撞把fbx模型的此项勾选上

unity跳跃按钮_Unity笔记(09200924)_第8张图片

效果

装备自旋转与拾取

https://blendswap.com/blend/26019#

unity跳跃按钮_Unity笔记(09200924)_第9张图片

使用blender导出fbx,注意坐标系

导入时勾选

unity跳跃按钮_Unity笔记(09200924)_第10张图片

导入场景摆好位置

unity跳跃按钮_Unity笔记(09200924)_第11张图片

unity跳跃按钮_Unity笔记(09200924)_第12张图片

gun添加Equipment的Tag与Layer

unity跳跃按钮_Unity笔记(09200924)_第13张图片

Player添加Player的Layer

3b242c3c10409660c5720fd5afd305bc.png

项目设置中PlayerEquipment图层取消勾选,忽略碰撞

unity跳跃按钮_Unity笔记(09200924)_第14张图片

新建Equipment.cs,与gun绑定

unity跳跃按钮_Unity笔记(09200924)_第15张图片

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会“收取”物品

unity跳跃按钮_Unity笔记(09200924)_第16张图片

人物的行走动画

https://assetstore.unity.com/packages/3d/props/polygon-starter-pack-156819

unity跳跃按钮_Unity笔记(09200924)_第17张图片

将前面的主角替换为素材包内人物

unity跳跃按钮_Unity笔记(09200924)_第18张图片

unity跳跃按钮_Unity笔记(09200924)_第19张图片

找到模型给它添加动画

unity跳跃按钮_Unity笔记(09200924)_第20张图片

因为这个素材自带动画,所以稍微剪辑一下,生成一个跑步时的动画和一个静止时的动画

unity跳跃按钮_Unity笔记(09200924)_第21张图片

修改Rig属性

unity跳跃按钮_Unity笔记(09200924)_第22张图片

Player添加Animation组件

unity跳跃按钮_Unity笔记(09200924)_第23张图片

修改Player.cs

 using System.Collections;using System.Collections.Generic;using UnityEngine;public class PlayerController : MonoBehaviour {
          private CharacterController cc;    private void Start () {
              ani=GetComponent();    }    private void Update () {
              if(Input.GetKey(KeyCode.W)){
                  ani.Play("Run");        }        if(Input.GetKeyUp(KeyCode.W)){
                  ani.Play("Stay");        }    }}

效果

unity跳跃按钮_Unity笔记(09200924)_第24张图片

2D按钮的拖动

随便来张卡牌和背景

unity跳跃按钮_Unity笔记(09200924)_第25张图片

unity跳跃按钮_Unity笔记(09200924)_第26张图片

将卡牌当做按钮

unity跳跃按钮_Unity笔记(09200924)_第27张图片

可以方便地添加按下时的效果

unity跳跃按钮_Unity笔记(09200924)_第28张图片

编写脚本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组件,绑定脚本里的函数

unity跳跃按钮_Unity笔记(09200924)_第29张图片

效果

unity跳跃按钮_Unity笔记(09200924)_第30张图片

技能CD效果

技能图片

3b8d78722bd417fd057996ac0e0ceb90.png

新建两个Image,一个Text

unity跳跃按钮_Unity笔记(09200924)_第31张图片

Mask的颜色改为偏灰色

unity跳跃按钮_Unity笔记(09200924)_第32张图片

新建一个脚本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里的函数

unity跳跃按钮_Unity笔记(09200924)_第33张图片

MaskCd选择对应的对象

unity跳跃按钮_Unity笔记(09200924)_第34张图片

MaskCdRaycast Target取消勾选,否则Skill不会响应点击

unity跳跃按钮_Unity笔记(09200924)_第35张图片

效果

unity跳跃按钮_Unity笔记(09200924)_第36张图片

你可能感兴趣的:(unity跳跃按钮)