【Unity 2D】角色移动、跳跃(二段跳或多段跳) 您所在的位置:网站首页 jumpforcepc操作 【Unity 2D】角色移动、跳跃(二段跳或多段跳)

【Unity 2D】角色移动、跳跃(二段跳或多段跳)

2023-04-17 20:44| 来源: 网络整理| 查看: 265

添加组件

首先给角色添加 刚体 和 碰撞器 组件

选择Dynamic可受重力影响,设置Gravity Scale增加重力比例,选择Continuous连续检测碰撞,选择Interpolate进行插值来平滑移动

此处添加了一个物理材质(Physics Material 2D)来消除摩擦力

添加脚本

然后给角色加上新建的脚本

横向移动

private Rigidbody2D rb; private Collider2D coll; public float speed = 8f; float xVelocity; void Start() { rb = GetComponent(); coll = GetComponent(); } void FixedUpdate() { Move(); } void Move() { xVelocity = Input.GetAxisRaw("Horizontal"); rb.velocity = new Vector2(xVelocity * speed,rb.velocity.y); }

Input.GetAxisRaw("Horizontal")直接获取-1,0,1

Input.GetAxis("Horizontal")获取的是[-1, 1],包括范围内的小数值

角色转向

void Move() { ... //镜面翻转 if (xVelocity != 0) { transform.localScale = new Vector3(xVelocity, 1, 1); } }

地面检测

public bool isOnGround; public LayerMask groundLayer; void FixedUpdate() { ... isOnGroundCheck(); } void isOnGroundCheck() { //判断角色碰撞器与地面图层发生接触 if (coll.IsTouchingLayers(groundLayer)) { isOnGround = true; } else { isOnGround = false; } } 自己创建一个新的Layer给地面

跳跃(二段跳)

public float jumpForce = 6f; int jumpCount;//跳跃次数 //按键状态 bool jumpPress; void Update() { if (Input.GetButtonDown("Jump") && jumpCount > 0) { jumpPress = true; } } void FixedUpdate() { ... Jump(); } void Jump() { //在地面上 if (isOnGround) { jumpCount = 1; } //在地面上跳跃 if (jumpPress && isOnGround) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); jumpCount--; jumpPress = false; } //在空中跳跃 else if (jumpPress && jumpCount > 0 && !isOnGround) { rb.velocity = new Vector2(rb.velocity.x,jumpForce); jumpCount--; jumpPress = false; } }

完整代码

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { private Rigidbody2D rb; private Collider2D coll; [Header("移动参数")] public float speed = 8f; float xVelocity; [Header("跳跃参数")] public float jumpForce = 6f; int jumpCount;//跳跃次数 [Header("状态")] public bool isOnGround; [Header("环境检测")] public LayerMask groundLayer; //按键设置 bool jumpPress; void Start() { rb = GetComponent(); coll = GetComponent(); } void Update() { if (Input.GetButtonDown("Jump") && jumpCount > 0) { jumpPress = true; } } void FixedUpdate() { isOnGroundCheck(); Move(); Jump(); } void isOnGroundCheck() { ////判断角色碰撞器与地面图层发生接触 if (coll.IsTouchingLayers(groundLayer)) { isOnGround = true; } else { isOnGround = false; } } void Move() { xVelocity = Input.GetAxisRaw("Horizontal"); rb.velocity = new Vector2(xVelocity * speed,rb.velocity.y); //镜面翻转 if (xVelocity != 0) { transform.localScale = new Vector3(xVelocity, 1, 1); } } void Jump() { //在地面上 if (isOnGround) { jumpCount = 1; } //在地面上跳跃 if (jumpPress && isOnGround) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); jumpCount--; jumpPress = false; } //在空中跳跃 else if (jumpPress && jumpCount > 0 && !isOnGround) { rb.velocity = new Vector2(rb.velocity.x,jumpForce); jumpCount--; jumpPress = false; } } }

角色动画切换(Blend Tree)

普普通通新青年:【Unity2D】角色动画(Blend Tree)



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有