유니티 프로젝트/Team Project1 - Fall Guys
폴가이즈 _ 점프 & 회피(참고)
SUGI_
2022. 7. 7. 02:04
https://www.youtube.com/watch?v=eZ8Dm809j4c&t=391s
if(rDown)
{
transform.position += moveVec * speed * runSpeed * Time.deltaTime;
}
else
{
transform.position += moveVec * speed * Time.deltaTime;
}
// 삼항연산자로 변경 가능
// bool 형태 조건 ? trun 일 때 값 : false 일때 값
// runFast가 참이라면 runSpeed를 곱해주고 거짓이라면 speed를 곱해준다.
transform.position += moveVec * (runFast ? speed * runSpeed : speed) * Time.deltaTime;
삼항연산자
if else 구문을 한줄로 간단하게 표현할 수 있기 때문에 인라인 if(inline-if)라고도 합니다.
if else 구문과 결과는 동일하지만 if else 구문은 여러줄로 작성되는 반면, 삼항연산자를 사용하면 한줄로 간단하게 표현할 수 있기 때문에 소스가 간결해집니다.
LookAt()
지정된 벡터를 향해서 회전을 시켜주는 함수
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 사용자의 입력값에 따라 좌우앞뒤로 이동하고 싶다.
// shift키를 누르면 빨리 달리고 싶다.
// jump키를 누르면 뛰고 싶다.
public class Player : MonoBehaviour
{
// 이동속도
public float speed = 10;
// 빨리달리기 속도
public float runSpeed = 2f;
float hAxis;
float vAxis;
Vector3 moveVec;
Animator anim;
Rigidbody rigid;
// 점프
bool jDown;
// 빨리 달리기
bool rDown;
bool isJump = false;
// Start is called before the first frame update
void Awake()
{
rigid = GetComponent<Rigidbody>();
anim = GetComponentInChildren<Animator>();
}
// Update is called once per frame
void Update()
{
GetInput();
Move();
Turn();
Jump();
}
void GetInput()
{
hAxis = Input.GetAxis("Horizontal");
vAxis = Input.GetAxis("Vertical");
jDown = Input.GetButton("Jump");
//shift 버튼에 RunFast 추가함
rDown = Input.GetButton("Runfast");
}
void Move()
{
// 이동하고싶다
moveVec = new Vector3(hAxis, 0, vAxis).normalized;
// 삼항연산자로 변경 가능
// bool 형태 조건 ? trun 일 때 값 : false 일때 값
transform.position += moveVec * (rDown ? speed * runSpeed : speed) * Time.deltaTime;
// Move 애니메이션 true
anim.SetBool("isMove", moveVec != Vector3.zero);
// 빨리 달리기
anim.SetBool("isRun", rDown);
}
void Turn()
{
// 자연스럽게 회전 = 나아가는 방향으로 바라본다
transform.LookAt(transform.position + moveVec);
}
void Jump()
{
// jump하고 잇는 상황에서 Jump하지 않도록 방지
if(jDown && !isJump)
{
rigid.AddForce(Vector3.up * 5, ForceMode.Impulse);
// Jump Trigger true 설정
anim.SetTrigger("doJump");
isJump = true;
}
}
}
지형물리
- Static 정적으로 변경 - 이유 collision Dection > Continuous 는 Static과 충돌할 때 효과적이다
- rigidbody - is Kinematic
- phtsics material
728x90