1梧疲、入場邮丰。
人物跳進(jìn)場景:下落動(dòng)畫 → 待機(jī)動(dòng)畫霸奕。
game視窗
動(dòng)畫機(jī)
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.transform.tag == Tags.Ground)
{
#region 接觸地面
if (!m_anim.GetBool("ground"))
{
m_anim.SetBool("ground", true);//動(dòng)畫機(jī)參數(shù)ground為是否在地面上
}
#endregion
}
}
2.跳躍。
待機(jī)動(dòng)畫 → 跳躍動(dòng)畫→ 下落動(dòng)畫 → 待機(jī)動(dòng)畫
跳躍
動(dòng)畫機(jī)
void Update()
{
#region 動(dòng)畫觸發(fā)
if (Input.GetAxis("Vertical") != 0)//有垂直方向鍵按住時(shí)实苞,觸發(fā)跳躍動(dòng)畫
{
if (!jumpOnce && !m_anim.GetBool("jump"))//jumponce:跳躍鍵按住時(shí)只跳一次的開關(guān)
{
jumpOnce = true;
m_anim.SetBool("jump", true);//jump:動(dòng)畫機(jī)的跳躍參數(shù)
}
}
else//沒有方向鍵按住時(shí)豺撑,觸發(fā)待機(jī)動(dòng)畫
{
jumpOnce = false;//跳躍開關(guān)復(fù)原
}
#endregion
}
向上跳躍為給人物一個(gè)垂直向上的力,物理運(yùn)動(dòng)相關(guān)的計(jì)算最好放在FixedUpdate()里黔牵。
private void FixedUpdate()
{
if (Input.GetAxis("Vertical") != 0)//有垂直方向鍵按住時(shí)聪轿,人物垂直移動(dòng)
{
if (!addForce && m_anim.GetBool("ground"))//在地面時(shí)才可以跳躍
{
addForce = true;//跳躍時(shí)只加一次力
m_rb2d.AddForce(Vector3.up * force);
}
}
else
{
addForce = false;//按鍵放開時(shí)力的開關(guān)恢復(fù)
}
}
3.1 移動(dòng)。
水平移動(dòng)
動(dòng)畫機(jī)
// Update is called once per frame
void Update()
{
#region 方向
if (Input.GetAxis("Horizontal") > 0)//右方按鍵按住時(shí)
{
transform.rotation = new Quaternion(0, 0, 0, 0);
}
else if (Input.GetAxis("Horizontal") < 0)//左方按鍵按住時(shí)
{
transform.rotation = new Quaternion(0, 1f, 0, 0);
}
#endregion
#region 移動(dòng)
if (Input.GetAxis("Horizontal") != 0)//有水平方向鍵按住時(shí)猾浦,人物水平移動(dòng)
{
transform.Translate(Vector3.right * Time.deltaTime * speed);
}
#endregion
#region 動(dòng)畫觸發(fā)
if (Input.GetAxis("Vertical") != 0)//有垂直方向鍵按住時(shí)屹电,觸發(fā)跳躍動(dòng)畫
{
if (!jumpOnce && !m_anim.GetBool("jump"))
{
jumpOnce = true;
m_anim.SetBool("jump", true);
}
}
if (Input.GetAxis("Horizontal") != 0)//有水平方向鍵按住時(shí),觸發(fā)走路動(dòng)畫
{
if (!m_anim.GetBool("walk"))
{
m_anim.SetBool("walk", true);
}
}
if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)//沒有方向鍵按住時(shí)跃巡,觸發(fā)待機(jī)動(dòng)畫
{
jumpOnce = false;//跳躍開關(guān)復(fù)原
if (m_anim.GetBool("walk"))
{
m_anim.SetBool("walk", false);
}
}
#endregion
}
3.2人物移動(dòng)到攝像機(jī)中央時(shí),人物開始在場景中向前進(jìn)牧愁,即路開始向后移動(dòng)素邪。
根據(jù)人物寬度調(diào)整了地磚
腳本掛在人物父物體。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[Tooltip("移動(dòng)速度")]
public float speed;
[Tooltip("向上跳躍的力")]
public float force;
/// <summary>
/// 通知地面開始向后移動(dòng)的標(biāo)記
/// </summary>
public static bool groundMove;
private Animator m_anim;
private Rigidbody2D m_rb2d;
/// <summary>
/// 用作跳躍時(shí)只給一次力
/// </summary>
public bool addForce;
/// <summary>
/// 用作跳躍動(dòng)畫在起跳鍵松開前只進(jìn)入一次
/// </summary>
public bool jumpOnce;
// Use this for initialization
void Start()
{
m_anim = GetComponentInChildren<Animator>();
m_rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
#region 方向
if (Input.GetAxis("Horizontal") > 0)//右方按鍵按住時(shí)
{
transform.rotation = new Quaternion(0, 0, 0, 0);
}
else if (Input.GetAxis("Horizontal") < 0)//左方按鍵按住時(shí)
{
transform.rotation = new Quaternion(0, 1f, 0, 0);
}
#endregion
#region 移動(dòng)
if (Input.GetAxis("Horizontal") != 0)//有水平方向鍵按住時(shí)猪半,人物水平移動(dòng)
{
if (Input.GetAxis("Horizontal") > 0)
{
if (transform.position.x < 0)//走到攝像機(jī)中央前兔朦,是人物移動(dòng)
{
transform.Translate(Vector3.right * Time.deltaTime * speed);
groundMove = false;
}
else//走到攝像機(jī)中央后,是地面向后移動(dòng)
{
groundMove = true;
}
}
else if (Input.GetAxis("Horizontal") < 0)
{
transform.Translate(Vector3.right * Time.deltaTime * speed);
}
}
#endregion
#region 動(dòng)畫觸發(fā)
if (Input.GetAxis("Vertical") != 0)//有垂直方向鍵按住時(shí)磨确,觸發(fā)跳躍動(dòng)畫
{
if (!jumpOnce && !m_anim.GetBool("jump"))
{
jumpOnce = true;
m_anim.SetBool("jump", true);
}
}
else
{
m_anim.SetBool("jump", false);
}
//跳躍動(dòng)畫播放完后進(jìn)入掉落動(dòng)畫沽甥,播放掉落動(dòng)畫時(shí),跳躍條件恢復(fù)
if (m_anim.GetCurrentAnimatorStateInfo(0).IsName("player_fall") && !m_anim.GetBool("jump"))
{
m_anim.SetBool("jump", false);
}
if (Input.GetAxis("Horizontal") != 0)//有水平方向鍵按住時(shí)乏奥,觸發(fā)走路動(dòng)畫
{
if (!m_anim.GetBool("walk"))
{
m_anim.SetBool("walk", true);
}
}
if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)//沒有方向鍵按住時(shí)摆舟,觸發(fā)待機(jī)動(dòng)畫
{
jumpOnce = false;//跳躍開關(guān)復(fù)原
if (m_anim.GetBool("walk"))
{
m_anim.SetBool("walk", false);
}
}
#endregion
}
private void FixedUpdate()
{
if (Input.GetAxis("Vertical") != 0)//有垂直方向鍵按住時(shí),人物垂直移動(dòng)
{
if (!addForce && m_anim.GetBool("ground"))//在地面時(shí)才可以跳躍
{
addForce = true;
m_rb2d.AddForce(Vector3.up * force);
}
}
else
{
addForce = false;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.transform.tag == Tags.Ground)
{
#region 接觸地面
m_anim.SetBool("ground", true);
#endregion
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.transform.tag == Tags.Ground)
{
#region 離開地面
m_anim.SetBool("ground", false);
#endregion
}
}
}
交流群 962257105