Unity2D基础之人物动画、移动、跳跃

Unity2D基础之人物动画、移动、跳跃

一、人物动画

从Window->Assets Store可以打开资源商店页面,可以选购一个免费的2D资源。本文就以这个骑士资源为角色.
Hero Knight

购买完成之后就导入Unity项目了。
通常购买的资源会有一个Demo项目运行,可以看看大概效果。动画、脚本等都有现成写好的。本文以学习为目的,所以会从头走一遍。

1.制作动画

找到Sprites文件夹,里面的HeroKnight文件,点击箭头可以看到这是已经帮我们做好了切割的。
我们先将第一张拖入Hierarchy,取名为Player
在这里插入图片描述

我们浏览一下下面的图片,可以发现:
0-6是站立,7-17是奔跑,18-36是攻击…
接着我们先选中0-6的图片,拖入到Player中
在这里插入图片描述

这时候就会出现一个保存的弹窗
在这里插入图片描述

我们选择好保存的路径和名称即可。

然后分别将其他的动作也以相同的方式创建动画,
在这里插入图片描述

可以看到左上角里动画已经都创建好了。

2.搭建场景

动画的切换会在角色控制的时候进行,这里我们先简单搭一个场景。

在Hierarchy里右键->2D Object-> Tilemap->Rectangular,就可以创建一个Tilemap
在这里插入图片描述

创建好后点击,左边的视图里就会出现很多小格子。
接着我们找到Environment文件夹下面的图片,简单的在格子里搭一个地板。将文件拖入即可。
[]

这时候点击运行,会发现人物会直接穿过地板掉下去。这是因为还没有碰撞检测。

3.设置碰撞检测

我们点击Player,在Inspector视图里选择Add Compoent,然后依次添加下面三个组件:

  1. RigidBody 2D 然后将Freeze Rotation z打勾

### 实现 Unity 2D 中虚拟摇杆控制角色跳跃功能 在 Unity 2D 游戏开发中,通过虚拟摇杆(Virtual Joystick)来实现角色跳跃是一项常见的需求。以下是关于如何设置并完成这一功能的具体方法。 #### 虚拟摇杆组件的选择与配置 为了实现虚拟摇杆的功能,可以使用第三方插件如 **EasyJoystick** 或者自己编写脚本创建自定义的虚拟摇杆逻辑。如果选择 EasyJoystick 插件,则可以通过拖拽的方式快速集成到场景中[^1]。对于手动实现的情况,通常需要两个 UI 图像对象分别表示背景圆盘和滑动指针,并监听触摸事件以计算方向向量。 ```csharp public class VirtualJoystick : MonoBehaviour { public Vector2 direction; // 输出的方向向量 private Image bgImage; private Image stickImage; void Start() { bgImage = GetComponent<Image>(); stickImage = transform.GetChild(0).GetComponent<Image>(); } void Update(){ HandleInput(); } void HandleInput(){ if (Input.touchCount > 0){ Touch touch = Input.GetTouch(0); if(IsPointerOverUIObject(touch.position)){ Vector2 delta = touch.position - (Vector2)bgImage.rectTransform.position; float magnitude = delta.magnitude; if(magnitude > bgImage.rectTransform.sizeDelta.x / 2f){ delta.Normalize(); } stickImage.rectTransform.anchoredPosition = new Vector2(delta.x, delta.y); direction = delta.normalized; } }else{ ResetStick(); } } bool IsPointerOverUIObject(Vector2 position){ PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); eventDataCurrentPosition.position = new Vector2(position.x,position.y); List<RaycastResult> results = new List<RaycastResult>(); EventSystem.current.RaycastAll(eventDataCurrentPosition,results); foreach(RaycastResult result in results){ if(result.gameObject.transform.IsChildOf(transform)) return true; } return false; } void ResetStick(){ stickImage.rectTransform.anchoredPosition = Vector2.zero; direction = Vector2.zero; } } ``` 上述代码展示了基础版的手写虚拟摇杆类 `VirtualJoystick` 的工作原理。它会检测触屏输入并将手指相对于摇杆中心的位置转换成标准化的方向矢量供其他部分调用。 #### 控制角色移动跳跃行为 一旦有了可用的虚拟摇棒数据流,就可以将其应用于玩家控制器上。下面是一个简单的 PlayerController 示例: ```csharp using UnityEngine; public class PlayerController : MonoBehaviour { Rigidbody2D rb; Animator anim; SpriteRenderer sr; public float speed = 5f; public float jumpForce = 7f; private bool facingRight = true; private bool isGrounded; public Transform groundCheckPoint; public LayerMask whatIsGround; public GameObject joystickGameObject; private VirtualJoystick vJoy; void Awake () { rb = GetComponent<Rigidbody2D>(); anim = GetComponent<Animator>(); sr = GetComponent<SpriteRenderer>(); vJoy = joystickGameObject.GetComponent<VirtualJoystick>(); } void FixedUpdate (){ MoveCharacter(vJoy.direction); CheckIfJumping(); AnimatePlayer(); } void MoveCharacter(Vector2 dir){ rb.velocity = new Vector2(dir.x * speed ,rb.velocity.y ); FlipIfNeeded(dir.x); } void CheckIfJumping(){ isGrounded = Physics2D.Linecast(transform.position,groundCheckPoint.position,whatIsGround); if(isGrounded && Mathf.Abs(vJoy.direction.y)>0.8 ){ Jump(); } } void Jump(){ rb.AddForce(new Vector2(0,jumpForce), ForceMode2D.Impulse); } void FlipIfNeeded(float horizontalMoveDirection){ if(horizontalMoveDirection != 0 && !Mathf.Approximately(horizontalMoveDirection,facingRight ? 1:-1)){ facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } } void AnimatePlayer(){ anim.SetFloat("Speed",Mathf.Abs(rb.velocity.x)); anim.SetBool("isGrounded",isGrounded); } } ``` 此段脚本实现了基于虚拟摇杆的角色水平运动以及垂直跳起动作。其中包含了基本的状态管理比如翻转朝向、动画参数更新等功能模块。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值