U2D动画控制

在这里插入图片描述
Ground状态切换到idle状态只需要在路径里面设置存在时间设置为1即动画全部播放就转换到下一个状态

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAnimator : MonoBehaviour
{
// Start is called before the first frame update
private Animator anim;
private Rigidbody2D rb;
private PlayerController1 playerController1;
void Start()
{
anim = GetComponent();//导入动画
rb = GetComponent();//使用刚体的速度
playerController1 = GetComponent();//引入角色的控制脚本
}

// Update is called once per frame
void Update()
{

    anim.SetFloat("speed", Mathf.Abs(rb.velocity.x));
    anim.SetBool("jump", playerController1.isjump);
    anim.SetFloat("velocityY", rb.velocity.y);
    anim.SetBool("ground", playerController1.isGround);
    
 
}

}
需要在原脚本添加isjump判断角色是否起跳
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController1 : MonoBehaviour
{
private Rigidbody2D rb;//刚体对象的名称
public float speed;//设置一个横向的速度
public float jumpForce;//设置跳跃的高度值
//这个变量在这里声明以后在unity中可以直接看到

[Header("Ground Check")]
public Transform groundCheck;//要用到Transform组件的position Transform相当于类型 后面跟名称 //这个数据直接拖动Player1下面的GroundCheck
                            //拖到显示框就能获取GroundCheck的position
public float checkRadius;//检测范围 在调试的时候一定要吧这个范围填上不然无法跳跃
public LayerMask groundlayer;//选择对应的图层 有很多图层这里我们选择Ground图层 LayerMask这里是Layer的变量类型

[Header("States Check")]//用了Header之后可以在界面看到参数
public bool isGround;
public bool isjump;
public bool canJump;
// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody2D>();//获取组件 后面是组件名称}{}内为函数参数
}

// Update is called once per frame
void Update()
{
    checkPress();
}
private void FixedUpdate()//这个是一秒钟执行50次 物理的检测就放到这个里面
{
    PhysicsCheck();
    Movement();
    jump();
   
}
void Movement()//角色移动部分
{
    //HorizontalInput按下A、D或左右键会从零开始慢慢到-1或1,用来使角色移动更加平滑
    //float HorizontalInput = Input.GetAxis("Horizontal");//包含小数,用这个办法是慢慢加速的移动 Horizontal是水平的意思
    float HorizontalInput = Input.GetAxisRaw("Horizontal");//不包含小数,速度平均;
    rb.velocity = new Vector2(HorizontalInput * speed, rb.velocity.y);//Vector2指的是二维向量 velocity是速度的意思 Vector为矢量 整句意思就是rb在这个二维矢量里面
    //将矢量分解为xy两个方向 x方向的速度为HorizontalInput * speed y方向的速度为原本Y的速度即保持不变 

    if (HorizontalInput != 0)
    {
        transform.localScale = new Vector3(HorizontalInput, 1, 1);//vector3是一个三维的向量,在使用localscale的时候我们希望向左移动的时候localscale为-1,
        //然后其实HorizontalInput向左的时候就是等于-1向右等于1 localscale也等于1相当于和HorizontalInput值重叠,就可以直接用HorizontalInput;

    }


}

void checkPress()
{
    if (Input.GetButtonDown("Jump") && isGround)//.GetButtonDown当这个按键按下的时候
    {   //Input.GetButtonDown("jump")判断输入 调用unity里面projectsetting 的jump
        
        canJump = true;

    }

}

void jump()
{
    if (canJump)
    {
        isjump = true;
        rb.velocity = new Vector2(rb.velocity.x, jumpForce);//如果按下了跳跃键,则Y轴的值为jumpForce
        rb.gravityScale = 4;  //下落的时候为4
        canJump = false;//加上跳跃检测不然会一直上市 按下之后jumpPressed为false就会下落

    }


}
void PhysicsCheck() {

    isGround = Physics2D.OverlapCircle(groundCheck.position,checkRadius,groundlayer);//以圆形为检测 
    if (isGround)
    {
        rb.gravityScale = 1;
        isjump = false;
    }               //起跳的时候为1

}
public void OnDrawGizmos()
{
    Gizmos.DrawSphere(groundCheck.position, checkRadius);//在相机上以groundcheck为圆心 checkRadius范围画圆显示在相机上
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值