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);
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章