using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
public class PlayerCtrl : MonoBehaviour
{
private Transform tr;
public float moveSpeed = 10.0f;
public float turnSpeed = 80.0f;
private Animation anim;
void Awake()
{
//제일먼저 호출되는 함수
//스크리브가 비활성화되있어도 호출되는 함수
}
private void OnEnable()
{
//두번째로 호출되는 함수
//스크립트가 활성화 될때마다 호출되는 함수
}
private void Start()
{
//세번째로 호출되는 함수
//Update함수가 호출되기 전에 호출되는 함수
//코루틴으로 호출될 수 있는 함수
//예)
//IEnumerator Start(){}
tr = GetComponent<Transform>();
anim = GetComponent<Animation>();
this.anim.Play("Idle");
//this.anim.clip = this.anim.GetClip("Idle");
//anim.Play();
}
private void FixedUpdate()
{
//일정간격으로 호출되는 함수 (기본값은 0.02초)
//물리엔진의 계산 주기와 일치
}
void Update()
{
//프레임마다 호출되는 함수
//호출 간격이 불규칙하다
//화면의 렌더링 주기와 일치 함
float h = Input.GetAxis("Horizontal"); // -1.0 ~ 0 ~ 1
float v = Input.GetAxis("Vertical"); // -1.0 ~ 0 ~ 1
float r = Input.GetAxis("Mouse X");
//Debug.Log("h=" + h);
//Debug.Log("v=" + v);
Debug.Log("r=" + r);
//this.tr.Translate(Vector3.forward * 1 * 0.01f , Space.Self); //로컬좌표 기준으로 이동
//this.tr.Translate(Vector3.forward * 1 * Time.deltaTime, Space.World); //월드좌표 기준으로 이동
//매 프레임마다 1유닛씩 이동 => 0.02초
//this.tr.Translate(Vector3.forward * 1 * Time.deltaTime);
//매 초마다 1유닛씩 이동
//프레임 레이트가 서로다른 기기에서도 개발자가 지정한 일정한 속도로 이동하기 위함
//this.tr.Translate(Vector3.forward * v * Time.deltaTime * moveSpeed); //방향 * 속도 * 시간 => forward = (0,0,1) // v => -1~ 1 사이의 값 => v를 사용해서 앞뿐만이 아닌 뒤로도 이동가능
//전후좌우 이동방향 벡터 계산
Vector3 moveDir = (Vector3.forward * v) + (Vector3.right * h); // right = (1,0,0)
//방향 * 속도 * 시간
this.tr.Translate(moveDir.normalized * moveSpeed * Time.deltaTime);
//Vector3.up 축으로 turnSpeed만큼
this.tr.Rotate(Vector3.up * r * this.turnSpeed);
//캐릭터 애니메이션 실행
PlayerAnim(h, v);
}
private void LateUpdate()
{
//Update함수가 종료된 후 호출 되는 함수
}
private void PlayerAnim(float h, float v)
{
//키보드 입력값을 기준으로 동작할 애니메이션 실행
if (v >= 0.1f)
{
//0.25f초간 이전 애니메이션과 실행할 애니메이션을 보간
this.anim.CrossFade("RunF", 0.25f); //전진 애니메이션 실행
}
else if (v <= -0.1f)
{
this.anim.CrossFade("RunB", 0.25f);//후진
}
else if (h >= 0.1f)
{
this.anim.CrossFade("RunR", 0.25f);//오른쪽
}
else if (h <= -0.1f)
{
this.anim.CrossFade("RunL", 0.25f);//왼쪽
}
else
{
this.anim.CrossFade("Idle", 0.25f);//기본
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletCtrl : MonoBehaviour
{
public float damage = 20.0f;
public float force = 1500f;
private Rigidbody rb;
void Start()
{
this.rb = this.GetComponent<Rigidbody>();
this.rb.AddForce(this.transform.forward * force);
Destroy(gameObject, 3f);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCam : MonoBehaviour
{
public Transform targetTr;
private Transform camTr;
//대상으로 부터 떨어질 거리
[Range(2.0f, 20.0f)]
public float distance = 10.0f;
//Y축으로 이동할 높이
[Range(0.0f, 10.0f)]
public float height = 2.0f;
//반응속도
public float damping = 10f;
//SmoothDamp에 사용할 속도 변수
private Vector3 velocity = Vector3.zero;
//카메라 LookAt OffSet
public float targetOffset = 2.0f;
void Start()
{
this.camTr = GetComponent<Transform>(); //Transform 객체 캐싱
}
private void LateUpdate()
{
//추적할 대상의 뒤쪽 distance만큼 이동
//높이를 height만큼 이동
Vector3 pos = this.targetTr.position +
(-targetTr.forward * distance) +
(Vector3.up * height);
//구면 선형 보간 함수를 사용해 부드럽게 위치를 변경
//시작위치, 목표 위치, 시간
//this.camTr.position = Vector3.Slerp(this.camTr.position, pos, Time.deltaTime * damping);
//시작위치, 목표위치, 현재속도, 목표 위치까지 도달할 시간
this.camTr.position = Vector3.SmoothDamp(this.camTr.position, pos, ref velocity, damping);
//Camera를 피벗 좌표를 향해 회전
this.camTr.LookAt(this.targetTr.position + (targetTr.up * targetOffset));
}
}