unity游戏开发学习之路一

c#游戏开发实战一-------角色移动脚本

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

public class runing : MonoBehaviour
{
    //定义变量
    private Animator animationer;//调取对象的动画组件
    private Rigidbody2D HeroRd;//调取对象的刚体,用于移动对象
    private Transform HeroRd1;//调取对象的位置组件,用于对对象进行位移操作
    private Collider2D Coll2d;//调取对象的碰撞体,用于碰撞检测
    public float MoveSpeed;//设置对象的移动速度
    public float JumpHeight;//设置对象的跳跃高度
    int count=0;//pass
    public LayerMask Ground;//地面层
    float h;//水平移动检测
    float v;//垂直移动检测
    bool push;//判断是否按下leftshift
    bool push_s;//判断是否按下s,用于判断是否处于蹲下状态
    bool runing_jump;//判断是否处于跳跃状态
    //定义函数
    //蹲下爬行函数
    public void sit(){
        
        if(push_s==true)
        {
            animationer.SetBool("Walk",false);
            animationer.SetBool("Sit",true);
            if(Input.GetKey(KeyCode.A)||Input.GetKey(KeyCode.D))
            { 
                animationer.SetBool("Crawl",true);
                if(Input.GetKey(KeyCode.A))
                {
                    HeroRd.velocity = new Vector2(h * MoveSpeed, HeroRd.velocity.y);
                    HeroRd1.localScale=new Vector3(-1,1,1);
                }
                else if(Input.GetKey(KeyCode.D))
                {
                    HeroRd.velocity = new Vector2(h * MoveSpeed, HeroRd.velocity.y);
                    HeroRd1.localScale=new Vector3(1,1,1);
                }
            }
            else
                animationer.SetBool("Crawl",false);
        }
        else
        {
            animationer.SetBool("Sit",false);
            animationer.SetBool("Crawl",false);
        }
    }
    //跳跃函数
   public void jump(){
        if (Input.GetKeyDown(KeyCode.Space)&&Coll2d.IsTouchingLayers(Ground))
        {
            animationer.SetBool("Run",false); 
            animationer.SetTrigger("Jump");
            HeroRd.AddForce(new Vector2(0, JumpHeight*3/2));  
        }
        else if(Coll2d.IsTouchingLayers(Ground))
                animationer.SetBool("Idle",true);
        else
            animationer.SetBool("Idle",false);
            // count++;
    }
    //攻击函数
    public void attack(){
        if(Input.GetKeyDown(KeyCode.J))
        {
            animationer.SetTrigger("Attack");
        }
    }
    //运动函数
    public void run(){
        //运动代码向左
            if(Input.GetKey(KeyCode.A)&&push==true)
            {
                HeroRd.velocity = new Vector2(h * MoveSpeed*2, HeroRd.velocity.y);
                HeroRd1.localScale=new Vector3(-1,1,1);
                animationer.SetBool("Run",true);
            }
            else if(Input.GetKey(KeyCode.A))
            {
                HeroRd.velocity = new Vector2(h * MoveSpeed, HeroRd.velocity.y);
                HeroRd1.localScale=new Vector3(-1,1,1);
                animationer.SetBool("Walk",true);
                animationer.SetBool("Run",false);
            }
            // else if(h==0)
            // {
            //     animationer.SetBool("Walk",false);
            //     animationer.SetBool("Run",false);
            // }     
            //运动代码向右
            if(Input.GetKey(KeyCode.D)&&push==true)
                {
                    HeroRd.velocity = new Vector2(h * MoveSpeed*2, HeroRd.velocity.y);
                    HeroRd1.localScale=new Vector3(1,1,1);
                    animationer.SetBool("Run",true);
                }
            else if(Input.GetKey(KeyCode.D))
            {
                HeroRd.velocity = new Vector2(h * MoveSpeed, HeroRd.velocity.y);
                HeroRd1.localScale=new Vector3(1,1,1);
                animationer.SetBool("Walk",true);
                animationer.SetBool("Run",false);
            }
            else if(h==0)
            {
                animationer.SetBool("Walk",false);
                animationer.SetBool("Run",false);
            }       
    }
    //唤醒函数
    void Awake()
    {
        HeroRd = transform.GetComponent<Rigidbody2D>();
        Coll2d=transform.GetComponent<Collider2D>();
        HeroRd1 = transform.GetComponent<Transform>();
        animationer = transform.GetComponent<Animator>();
        
    }
    //更新函数
    void Update()
     {
        h = Input.GetAxis("Horizontal");
        v=Input.GetAxis("Vertical");
        
        //攻击代码
        attack();
        // else if(Input.GetKeyDown(KeyCode.U))
        // {
        //     animationer.SetTrigger("Attack1");
        // }
        // //判断shift按下状态
        if(Input.GetKey(KeyCode.LeftShift))
            push=true;
        else
            push=false;
        //判断蹲下状态
        if(Input.GetKey(KeyCode.S))
            push_s=true;
        else
            push_s=false;

        if (Coll2d.IsTouchingLayers(Ground))
            runing_jump=false;
        else
            runing_jump=true;
        //运动代码。
        if(push_s==false&&Coll2d.IsTouchingLayers(Ground))
        {
            run();
        }
        else{
            animationer.SetBool("Walk",false);
            animationer.SetBool("Run",false);
        }
        //跳跃代码
        jump();
        //控制蹲下爬行
        sit();    
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值