Unity笔记(0920-0924)

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

原视频

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

新建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<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

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

新建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里的函数

MaskCd选择对应的对象

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

效果

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值