👨💻个人主页:@元宇宙-秩沅
hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
本文由 秩沅 原创
收录于专栏:unity游戏制作
🎶版本: 为 Ltp 2021年版本的unity
🎶类型: 简单2D类冒险游戏
🎶目的: 熟悉掌握基本unityAPI
🎶视频教程:【2023小白狂飙unity2D冒险类游戏制作【mango的冒险】】
音频资源直接去unity的商店里下载免费的资源即可
细节:勾选Freeze Rotation Z
作用:不会倾倒
//通过刚体组件实现物体的移动,我们只需要将刚体速度的大小和方向进行赋值即可//mangoMovex 和 mangoMoveY 都是vector2 类型的变量mangoMovex = Vector2.right * x * SpeedVauel; //x轴的速度mangoMovey = new Vector2(0, Rmango.velocity.y); //y轴的速度//速度向量的合成,有大小并且有方向的Rmango.velocity = mangoMovex + mangoMovey;
// 效果: 可以进行移动,但是方向不变,定向的,
MGanimator.SetFloat("Run", Mathf.Abs(Rmango .velocity .x));
转向的三种方法 | 注释 |
---|---|
Rotation | 180度旋转 |
Scale | 正负的切换 |
Flip | 勾选 |
if (x > 0){transform.rotation = new Quaternion(0, 0, 0, 0);}else if (x < 0){transform.rotation = new Quaternion(0, 180, 0, 0);}
动画的逻辑:
奔跑动画在跳跃状态时才切换到跳跃动画
禁止状态在跳跃状态时也切换到跳跃动画
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Rendering;
using UnityEngine;//-----------------------
//--作用:mango的移动
//-----------------------
public class mangoMove : MonoBehaviour
{// Start is called before the first frame updateprivate float x;private float y;private Rigidbody2D Rmango;private Vector2 mangoMovex,mangoMovey;private float SpeedVauel = 5;public float JumpSpeed = 1000;private Animator MGanimator;private bool isGrounp;void Start(){Rmango = GetComponent();MGanimator = GetComponent();}private void Update(){x = Input.GetAxis("Horizontal"); // 按AD键获取类型为Float的数值作为系数范围为【-1,1】//当按下A的时候 x是负数 ,按下D的时候x是正数if (x > 0){transform.rotation = new Quaternion(0, 0, 0, 0);}else if (x < 0){transform.rotation = new Quaternion(0, 180, 0, 0);}//当按下空格键和 符合 在地面的条件时if ( Input.GetButtonDown ("Jump") && isGrounp ) { Rmango.AddForce(Vector2.up * JumpSpeed );MGanimator.SetBool("Jump", true);}else if( isGrounp == true ){MGanimator.SetBool("Jump", false );}}void FixedUpdate(){Move();}private void Move(){//通过刚体组件实现物体的移动,我们只需要将刚体速度的大小和方向进行赋值即可//mangoMovex 和 mangoMoveY 都是vector2 类型的变量mangoMovex = Vector2.right * x * SpeedVauel; //x轴的速度mangoMovey = new Vector2(0, Rmango.velocity.y); //y轴的速度//速度向量的合成,有大小并且有方向的Rmango.velocity = mangoMovex + mangoMovey;if (isGrounp == true ) //是否在地面上{MGanimator.SetFloat("Run", Mathf.Abs(Rmango .velocity .x));}else{MGanimator.SetFloat("Run", 0);}}//碰撞器方法private void OnCollisionEnter2D(Collision2D collision){isGrounp =collision . gameObject.CompareTag("grounp");Debug.Log(" " + isGrounp);}private void OnCollisionExit2D(Collision2D collision) //也可以说是跳跃状态的时候{isGrounp = false;Debug.Log(" " + isGrounp);}
}
🎶🎶我们继续后面的制作。
⭐【2023unity游戏制作-mango的冒险】-2.开始画面API制作
⭐【unity游戏制作-mango的冒险】-1.场景搭建
⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)
⭐本站最全-unity常用API大全(万字详解),不信你不收藏
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!