iT邦幫忙

2023 iThome 鐵人賽

DAY 9
0
Software Development

Unity遊戲開發系列 第 9

DAY09 角色動畫Animator

  • 分享至 

  • xImage
  •  

Animation
能夠控制動畫在每一幀的動作並指派給動畫元件並透過腳本控製播放
https://ithelp.ithome.com.tw/upload/images/20230924/20162530FfWrokIfGo.png

Animator
https://ithelp.ithome.com.tw/upload/images/20230924/20162530QgaBCbueo5.png

https://ithelp.ithome.com.tw/upload/images/20230924/20162530NbSALMOU8Y.png

 public Animator aniplay;

待更

實作

加入移動動畫

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class playcontrol : MonoBehaviour
{
    public float movechage = 50f;
    Rigidbody2D rigid2D;
    [SerializeField]
    private float speed;
    [SerializeField]
    private float speed_x_constraint;
    public Animator aniplay;
    public int jumpcheck=0;
    private Vector2 moveVelocity;
    void Start()
    {
        rigid2D = this.gameObject.GetComponent<Rigidbody2D>();
    }
 void Update()
    {
        float deltatime = Time.deltaTime;
        bool iswalking = false;
        //跳躍
        if (Input.GetKeyDown(KeyCode.Space) && jumpcheck<2)
        {
            rigid2D.AddForce(new Vector2(0, 200), ForceMode2D.Impulse);
            jumpcheck +=1;
        }
        //右移動
         if (Input.GetKey(KeyCode.D))
        {
            iswalking = true;
            rigid2D.velocity = new Vector2(speed_x_constraint, rigid2D.velocity.y);          
        }
         //左移動
         if(Input.GetKey(KeyCode.A))
        {
            iswalking = true;
            rigid2D.velocity = new Vector2(-speed_x_constraint, rigid2D.velocity.y);          
        }   
        //移動動畫
        if (iswalking)
        {
            if (aniplay.GetInteger("startw") == 0) 
                aniplay.SetInteger("startw", 1);           
        }
        else 
        {
            if(aniplay.GetInteger("startw") == 1) 
                aniplay.SetInteger("startw", 0);
        }
    }
    void OnCollisionEnter2D(Collision2D coll) {
        if (coll.gameObject.tag == "trap") {
            hp -= 1;    
            rigid2D.AddForce(new Vector2(0, 100), ForceMode2D.Impulse);
         }
        if (coll.gameObject.tag == "Scenesobject") {
            jumpcheck = 0;
        }       
    }
  }

參考資料:
https://www.youtube.com/watch?v=aIOYDtJnJI0


上一篇
DAY08 Unity生成物件
下一篇
DAY10 列舉Enumeration
系列文
Unity遊戲開發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言