unity --15 练习制作一个2d游戏sunny land

刚体与碰撞体

前面弄了瓦片地图。
但此时显示出来的东西本质上还是图片。
怎么让图片能成为独立的物体?

需要添加两个部分,一个是让静态的图片能够模拟物理效果,各类刚体。
在这里插入图片描述
另一个让静态的图片能够模拟碰撞效果,各类碰撞体。
在这里插入图片描述
特别的,对瓦片地图,需要选用针对性的碰撞体,这样的好处是unity自动划分好碰撞区域,无需手动一个一个添加。
在这里插入图片描述
在这里插入图片描述

控制移动

在edit菜单栏里有一项project setting,管理input
在这里插入图片描述
①获取这个输入管理中的设定值,使用 Input.GetAxis(“Horizontal”); 函数返回值是-1(水平向左),0(没动),+1(水平向右),返回值是一个区间,-1到0,0到+1,所以是浮点数。

②根据返回值判断方向,移动。

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

public class playerContral : MonoBehaviour 
{
    public Rigidbody2D rd;
 
	// Use this for initialization
	void Start () 
	{
		
	}
	
	// Update is called once per frame
	void Update () 
	{
        Move();
	}

    void Move()
    {
        float hori;
        hori= Input.GetAxis("Horizontal");

        if (hori!=0)
        {
            rd.velocity = new Vector2(hori * 1, rd.velocity.y);
        }
    }
}

移动流程和我想象的不一样,首先它不是利用transform来移动,而是用的刚体的velocity属性,区别在于后者移动时能模拟物理效果(重力,摩擦力);特别的velocity属性用的是直接赋值的方法;其次它判断按键不是用的类似 Input.GetKey(KeyCode.RightArrow);而是直接读取输入管理设定值。

运行效果:
在这里插入图片描述
为什么会这样? 因为在刚体设置里没有禁止z轴旋转
在这里插入图片描述
假设感觉移动速度慢了,还可以添加一个速度参数

            rd.velocity = new Vector2(hori *speed, rd.velocity.y);

控制图片方向

原理是变换组件下的缩放scale参数,当x=-1时,图片会“翻面”

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

public class playerContral : MonoBehaviour 
{
    public Rigidbody2D rd;
    public float speed;
	// Use this for initialization
	void Start () 
	{
		
	}
	
	// Update is called once per frame
	void Update () 
	{
        Move();
	}

    void Move()
    {
        float hori = Input.GetAxis("Horizontal"); ;

        //和上句区别在于,后者能把-1到0的区间直接换成-1,即facedirection的值只能是-1,0,+1
        float facedirection = Input.GetAxisRaw("Horizontal");
        if (hori != 0)
        {
            rd.velocity = new Vector2(hori * speed, rd.velocity.y);
        }
        if (facedirection != 0)
        {
            transform.localScale = new Vector3(facedirection, 1, 1);
        }
    }
}

可以加上平滑运动

            rd.velocity = new Vector2(hori * speed*Time.deltaTime, rd.velocity.y);

此时需要调整speed的值,大约80比较好。

添加跳跃

依葫芦画瓢,jump仍然是读取的输入管理,而不是直接硬编码。
GetButtonDown还有个好处,它只触发一次,哪怕是按住不放,不会出现按住不放,人物就一直飞上天。
连续跳,那就是多按几次,但是缺点是人物就一直飞上天。

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

public class playerContral : MonoBehaviour 
{
    public Rigidbody2D rd;
    public float speed;
    public float jumpforce;
	// Use this for initialization
	void Start () 
	{
		
	}
	
	// Update is called once per frame
	void Update () 
	{
        Move();
	}

    void Move()
    {
        float hori = Input.GetAxis("Horizontal"); ;

        //和上句区别在于,后者能把-1到0的区间直接换成-1,即facedirection的值只能是-1,0,+1
        float facedirection = Input.GetAxisRaw("Horizontal");
        if (hori != 0)
        {
            rd.velocity = new Vector2(hori * speed*Time.deltaTime, rd.velocity.y);
        }
        if (facedirection != 0)
        {
            transform.localScale = new Vector3(facedirection, 1, 1);
        }

        if (Input.GetButtonDown("Jump"))
        {
            rd.velocity = new Vector2(rd.velocity.x, jumpforce * Time.deltaTime);
        }
    }
}

添加动画

①首先给物体添加Animator组件
②在window菜单中打开Animation窗口,把多张图片拖拽进去,不要忘记修改pixels per unit
在这里插入图片描述
感觉动画太快了,调整samples。
添加其他的动画:
在这里插入图片描述
③利用Animator窗口来管理若干个动画
在这里插入图片描述
在这里插入图片描述
怎么判断它会在idel和run两个动画之间切换?
添加参数
在这里插入图片描述
添加条件,如果跑动速度大于0.1
在这里插入图片描述
取消动画的退出时间,让它立刻转换,
在这里插入图片描述
④ 怎么设置runing这个参数? 需要在代码中编写,也就是说管理整个动画的切换,一部分是依靠animator窗口,一部分是依靠代码

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

public class playerContral : MonoBehaviour 
{
    public Rigidbody2D rd;
    public float speed;
    public float jumpforce;
    public Animator anim;
	// Use this for initialization
	void Start () 
	{
		
	}
	
	// Update is called once per frame
	void Update () 
	{
        Move();
	}

    void Move()
    {
        float hori = Input.GetAxis("Horizontal"); ;

        //和上句区别在于,后者能把-1到0的区间直接换成-1,即facedirection的值只能是-1,0,+1
        float facedirection = Input.GetAxisRaw("Horizontal");

        //角色水平移动
        if (hori != 0)
        {
            rd.velocity = new Vector2(hori * speed*Time.deltaTime, rd.velocity.y);
            //配合Animator窗口,设置参数runing的值,无论向左向右,绝对值都为1,即runing的值为1
            //runing的值为1,满足Animator窗口设定的切换条件
            anim.SetFloat("runing",Mathf.Abs(facedirection));
        }
        if (facedirection != 0)
        {
            transform.localScale = new Vector3(facedirection, 1, 1);
        }
        //角色跳跃
        if (Input.GetButtonDown("Jump"))
        {
            rd.velocity = new Vector2(rd.velocity.x, jumpforce * Time.deltaTime);
        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值