上一次製作了2D動畫 http://baijiahao.baidu.com/builder/preview/s?id=1625053888991433390
下面就需要設置狀態,讓主角可以在地圖上任意方向的行走
RPG遊戲裡的主角往往都是待機和跑步兩種狀態,所以需要複製一份狀態
可以刪除所有Animator Controller,保留Animation
右鍵Show in Explorer然後複製並且F2重命名一下
把一個動畫狀態拖到屬性面板可以自動生成一個Animator Controller,用來打開動畫狀態機先Window——Animation,打開老式狀態機Animation
複製狀態
選擇Stop_up,設置第一幀是關鍵幀,Ctrl+C
選擇run_up,Ctrl+V,以此類推
執行window——Animatior,打開狀態機,新建兩個Blend Tree
分別重命名Stop和run,兩種狀態
切換到Parameters。創建兩個Float變量(input_x,input_y)和一個Bool變量(runing)
打開Stop的Blend Tree狀態樹,設置Blend Type的狀態
Parameters參數 是剛才創建的Float變量input_x和input_y
Motion下添加4個上下左右四個狀態,設置坐標位置
同樣完成run的Blend Tree設置。
最後Mask Transition狀態連線。
分別設置一下吧,主要還是Conditions的狀態
創建Script文件夾,再裡面創建C#腳本,命名Play,拖到player屬性面板
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Play : MonoBehaviour
{
Rigidbody2D rb;
Animator anim;
//Use this for ini tialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
//Update is called once per frame
void Update()
{
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (movement_vector != Vector2.zero)
{
anim.SetBool("runing", true);
anim.SetFloat("input_x", movement_vector.x);
anim.SetFloat("input_y", movement_vector.y);
}
else
{
anim.SetBool("runing", false);
}
rb.MovePosition(rb.position + movement_vector * Time.deltaTime * 80f);
}
}
為player添加2D剛體,設置重心為0
測試遊戲,攝像機沒有跟隨角色移動,所以需要設置攝像機
繼續在Script文件夾創建腳本CameraMovement//相機移動
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMovement : MonoBehaviour {
public float Speed = 0.03f;
public Transform target;
Camera camera;
// Use this for initialization
void Start () {
camera = GetComponent<Camera>();
}
// Update is called once per frame
void Update () {
camera.orthographicSize = (Screen.height / 0.65f) / 4f; //設置正交攝像機
if (target) {
transform.position = Vector3.Lerp(transform.position,target.position,Speed)+new Vector3(0,0,-10);
}
}
}
然後把Main Camear掛到player上,測試遊戲,主角可以在地圖上任意行走了。
後面介紹利用觸發器做場景傳送