유니티 기초

pixel_sword

다모아 2023. 8. 3. 14:35
        float h = Input.GetAxisRaw("Horizontal"); //-1 0 1
        Debug.LogFormat("=> {0}", h);

 

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

public class HeroController : MonoBehaviour
{
    private Animator anim;
    private Rigidbody2D rBody2D;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.GetComponent<Animator>();
        this.rBody2D = this.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal"); //-1 0 1
        Debug.LogFormat("=> {0}", h);
        //좌우 반전 , 왼쪽 h값이 -1
        if(h != 0)
        {
            this.transform.localScale = new Vector3(h, 1, 1);
        }

        this.anim.SetInteger("Direction", (int)h);
        
    }
}

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

public class HeroController : MonoBehaviour
{
    private Animator anim;
    private Rigidbody2D rBody2D;
    public float moveForce = 1f;
    public float moveSpeed = 1f;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.GetComponent<Animator>();
        this.rBody2D = this.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal"); //-1 0 1
        Debug.LogFormat("=> {0}", h);
        //좌우 반전 , 왼쪽 h값이 -1
        if (h != 0)
        {
            this.transform.localScale = new Vector3(h, 1, 1);
        }
        this.anim.SetInteger("Direction", (int)h);

        //2번째 방법
        var dir = new Vector2(h, 0);
        //rigidbody로 이동하는 방법
        //방향 * 힘 ..> 오류
        //if(h != 0)
        //{
        //    var force = new Vector3(h, 0, 0) * moveForce;
        //    this.rBody2D.AddForce(force);
        //}
        //else
        //{
        //    this.rBody2D.velocity = Vector2.zero;
        //}
        //this.rBody2D.AddForce(new Vector3(h, 0, 0) * moveForce);


        //rigidbody로 이동하는 방법
        //this.rBody2D.MovePosition(방향 * 속도 * 시간);
        //this.rBody2D.MovePosition(dir * this.moveSpeed * Time.deltaTime);

        //var movement = dir * this.moveSpeed * Time.deltaTime;
        //this.rBody2D.MovePosition(this.rBody2D.position + movement);
        //Debug.LogFormat("movement : {0}",movement);
        //anim.SetFloat("Speed", Mathf.Abs(movement.x));
        //Debug.LogFormat("Mathf.Abs(movement.x): {0}", Mathf.Abs(movement.x));

        //velocity를 직접 변경하는 방법
        //float velocityX = moveSpeed * Input.GetAxisRaw("Horizontal");
        //this.rBody2D.velocity = new Vector2(velocityX, 0);
        //Debug.Log(velocityX);
        //anim.SetFloat("Speed", Mathf.Abs(this.rBody2D.velocity.x));

    }
}

 

        //2번째 방법
        var dir = new Vector2(h, 0);

        this.transform.Translate(dir * this.moveForce * Time.deltaTime);
        this.anim.SetInteger("Direction", (int)dir.x);

 

Scale이 x,y,z 전부 0이상 이어야 배경화면 앞으로 나온다..

 

if(collision.gameObject.tag == "Ground")

태그 설정시에는 Tag를 바꿔줘야한다.

 

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

public class HeroController : MonoBehaviour
{
    private Rigidbody2D rBody2D;
    private Animator anim;

    public float moveSpeed = 5f;
    public float jumpForce = 5f;
    public float moveForce = 2f;
    private bool isJump = false;
    private float direction;
    // Start is called before the first frame update
    void Start()
    {
        this.rBody2D = this.GetComponent<Rigidbody2D>();
        this.anim = this.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        this.direction = Input.GetAxisRaw("Horizontal"); // -1 0 1

        if (Input.GetKeyDown(KeyCode.Space))
        {
            this.rBody2D.AddForce(Vector2.up * this.jumpForce, ForceMode2D.Impulse);
            if (this.isJump == false)
                this.isJump = true;
        }

        if(this.isJump)
        {
            if(this.rBody2D.velocity.y > 0)
            {
                this.anim.SetInteger("State", 2);
            }
            else
            {
                this.anim.SetInteger("State", 3);
            }
        }

        if(direction != 0)
        {
            if (!this.isJump)
                this.anim.SetInteger("State", 1);

            this.transform.localScale = new Vector3(direction, 1, 1);

            Vector2 force = Vector2.zero;

            if(this.isJump)
            {
                force = new Vector2(direction, 0) * this.moveForce * 0.5f;
                if (Mathf.Abs(force.x) > 15f)
                    force.x = 15f * direction;

                this.rBody2D.AddForce(force);
            }
            else
            {
                force = new Vector2(direction, 0) * this.moveForce;
                if (Mathf.Abs(force.x) > 15f)
                    force.x = 15f * direction;

                this.rBody2D.AddForce(force);
            }
        }
        else
        {
            if (!this.isJump)
                this.anim.SetInteger("State", 0);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "Ground")
        {
            if(this.isJump)
            {
                this.isJump = false;

                if(this.direction != 0)
                {
                    //run
                    this.anim.SetInteger("State", 1);
                }
                else
                {
                    this.anim.SetInteger("State", 0);
                }
            }
        }
    }
}

마지막

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

public class HeroController : MonoBehaviour
{
    private float h;
    public float moveSpeed = 3f;
    public float jumpForce = 5f;
    private bool isJump = false;

    private Animator anim;
    private Rigidbody2D rBody2D;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.GetComponent<Animator>();
        this.rBody2D = this.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        this.h = Input.GetAxisRaw("Horizontal"); // -1 , 0 , 1

        if(h != 0)
        {
            this.transform.localScale = new Vector3(h, 1, 1);
            this.anim.SetInteger("State", 1);
        }
        else
            this.anim.SetInteger("State", 0);

        //리지드바디 X 이동
        Vector2 dir = new Vector2(h, 0);
        this.transform.Translate(dir * this.moveSpeed * Time.deltaTime);

        if(Input.GetKeyDown(KeyCode.Space))
        {
            this.rBody2D.AddForce(Vector2.up * this.jumpForce, ForceMode2D.Impulse);

            if (this.isJump == false)
                this.isJump = true;
        }

        if(this.isJump)
        {
            if(this.rBody2D.velocity.y > 0)
            {
                this.anim.SetInteger("State", 2);
            }
            else
            {
                this.anim.SetInteger("State", 3);
            }
        }
    }

    public void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "Ground")
        {
            this.isJump = false;
            this.anim.SetInteger("State", 0);
        }
    }
}

2D는 선 누르고 Transition Duration 을 0으로 해주는게 좋고 Has Exit Time도 꺼주는게 좋다.

블렌딩 어쩌구.. 끝난 뒤에 실행 어쩌구..