Move_Velocity

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

public class Move_Velocity : MonoBehaviour
{
    int count = 0;  // 초(횟수)

    // 리지드바디2D 변수(객체)선언-전역변수
    Rigidbody2D rb;

    void Start()
    {
        // 리지드바디2D 컴포넌트 가져오기
        rb = GetComponent<Rigidbody2D>();

        // 중력을 0으로하고, 충돌시 회전하지않도록 설정
        rb.gravityScale = 0;
        rb.constraints = RigidbodyConstraints2D.FreezeRotation;
    }

    void Update()   // 계속 호출(실행)된다 - 비정기적
    {
        
    }
    void FixedUpdate() // 계속 호출(실행)된다-1초에 50번 실행
    {
        // 1초동안 5만큼 이동, 1초가 지나면 정지

        if (count == 0)
        {
            rb.velocity = new Vector2(5, 0);
        }
        if (count == 50)
        {
            rb.velocity = new Vector2(0, 0);
        }


        count++;
    }
}

Move_AddForce

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

public class Move_AddForce : MonoBehaviour
{
    // 키보드 a키와 s키의 움직임(AddForce())

    public float power = 10.0f;

    Rigidbody2D rb;

    void Start()
    {
        // 리지드바디2D 컴포넌트 가져오기
        rb = GetComponent<Rigidbody2D>();

    }

    void Update()
    {

    }
    private void FixedUpdate()
    {
        // a키가 눌린 순간 오른쪽 이동(순간적인 힘)
        if (Input.GetKeyDown(KeyCode.A))
        {
            rb.AddForce(transform.right * power, ForceMode2D.Impulse);
        }
        // s키가 눌린 순간 오른쪽 이동(순간적인 힘)
        else if (Input.GetKeyDown(KeyCode.S))
        {
            rb.AddForce(transform.right * power, ForceMode2D.Force);
        }
    }
}

Velocity_AddForce

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

public class Velocity_AddForce : MonoBehaviour
{
    // 좌우방향키를 눌렀을 때 좌우방향으로 이동하다가
    // 스페이스 키를 누르면 점프하기
    // velocity와 AddForce() 동시 사용

    // 좌우방향키 입력: GetAxisPaw()-변수명: axisH
    // 좌우방향으로 갈 때 속도: speed = 5
    // 점프할 때 점프력 : power = 10
    float axisH = 0;
    public float speed = 5.0f;
    public float power = 10.0f;

    // 리지드바디2D 변수 선언
    Rigidbody2D rb;

    private void Start()
    {
        // 리지드바디2D 컴포넌트 가져오기
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        // 좌우방향키 감지
        axisH = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
        // 좌우방향 이동하기 (velocity)
        rb.velocity = new Vector2(axisH * 5.0f, rb.velocity.y);
        GetComponent<SpriteRenderer>().flipX = (axisH < 0);

        // 스페이스 키 눌린 순간 위로 점프 (AddForce)
        // (KeyCode.Space)/ ("space")
        if (Input.GetKeyDown(KeyCode.Space))
        {
            rb.AddForce(transform.up * power, ForceMode2D.Impulse);
        }
    }
}

'----------고1---------- > 게임엔진' 카테고리의 다른 글

[고1 게임엔진] 09 - (3)  (0) 2023.09.13
[고1 게임엔진] 09 - (2)  (3) 2023.09.11
[고1 게임엔진] 09 - (1)  (0) 2023.09.04
[고1 게임엔진] 08 - (1)  (0) 2023.08.30
[고1 게임엔진] 08 - (3)  (0) 2023.08.30