Move_Car
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move_Car : MonoBehaviour
{
Rigidbody2D rb; // 리지드바디 2D형 변수 선언 (객체)
void Start()
{
// 리지드바디 2D 컴포넌트 가져오기
rb = GetComponent<Rigidbody2D>();
// 중력을 0으로 (무중력)
//rb.gravityScale = 0;
// 충돌 시 회전시키지 않기
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
}
void Update()
{
if (Input.GetButton("Jump"))
{
//rb.velocity = new Vector2(0, 5);
rb.AddForce(new Vector2(0, 10));
}
}
}
Move_Flip
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move_Flip : MonoBehaviour
{
Rigidbody2D rb; // 리지드바디 2D형 변수 선언 (객체)
public float speed = 3; // 좌우방향 이동시 이동속도
float axisH = 0; // 키보드 좌우방향키 입력 확인
void Start()
{
// 리지드바디 2D 컴포넌트 가져오기
rb = GetComponent<Rigidbody2D>();
// 충돌 시 회전시키지 않기
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
}
void Update()
{
// 좌우방향키 입력 감지
axisH = Input.GetAxisRaw("Horizontal");
if(axisH > 0) // 오른쪽 방향키를 눌렸다면
{
transform.localScale = new Vector2(1, 1);
}
else if(axisH > 0) // 왼쪽 방향키를 눌렸다면
{
transform.localScale = new Vector2(-1, 1); // 좌우반전됨
}
}
void FixedUpdate()
{
// 좌우방향 이동
rb.velocity = new Vector2(axisH * speed, 0);
}
}
Move_Jump
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move_Jump : MonoBehaviour
{
// velocity, AddForce()
public float speed = 5; // 좌우방향 이동속도
public float jumpPower = 3; // 점프력
float vx = 0; // x축방향으로 이동하는 이동량
bool leftFlag = false; // 반전여부
bool spaceFlag = false; // 스페이스 키 누르고 있는지 여부
Rigidbody2D rb;
SpriteRenderer sr;
void Start() // 실행하자마자 한번만 실행(초기화)
{
// 리지드바디 2D 컴포넌트 가져오기
rb = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
// 충돌 시 회전시키지 않기
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
}
void Update() // 계속 실행(호출)된다 (1초에 60번호출-PC사양에 다라 호출되는 간격이 달라진다.)
{
vx = 0; // 초기화
// 키보드 키 입력 감지
if(Input.GetKey(KeyCode.RightArrow))
{
vx = speed;
leftFlag = false;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
vx = -speed;
leftFlag = true;
}
if(Input.GetKey(KeyCode.Space))
{
spaceFlag = true;
}
else
{
spaceFlag = false;
}
}
void FixedUpdate() // 계속 실행(호출)된다 (1초에 50번 호출)
{
// 좌우방향이동
rb.velocity = new Vector2(vx, rb.velocity.y);
Debug.Log(rb.velocity.y);
// 반전하기
sr.flipX = leftFlag;
// 점프하기
if (spaceFlag == true)
{
rb.AddForce(new Vector2(0, jumpPower), 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 - (2) (0) | 2023.08.28 |