활동 내용
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private Rigidbody playerRigidbody;
public float speed = 8f;
void Start() {
playerRigidbody = GetComponent<Rigidbody>();
}
void Update() {
float xInput = Input.GetAxis("Horizontal");
float zInput = Input.GetAxis("Vertical");
float xSpeed = xInput * speed;
float zSpeed = zInput * speed;
Vector3 newVelocity = new Vector3(xSpeed, 0f, zSpeed);
playerRigidbody.velocity = newVelocity;
}
public void Die() {
gameObject.SetActive(false);
}
}
플레이어 캐릭터의 이동을 만들었다. 물리값을 가지게 하는 리지드바기 컴포넌트를 적용했다. 입력 값을 기반으로 캐릭터를 움직이도록 수평 수직 입력을 받고 그에 맞는 속도를 계산하여 움직이게 했다. 게임 오브젝트가 동작을 정지하게하는 die 메서드를 제작했다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour {
public float speed = 8f;
private Rigidbody bulletRigidbody;
void Start() {
bulletRigidbody = GetComponent<Rigidbody>();
bulletRigidbody.velocity = transform.forward * speed;
Destroy(gameObject, 3f);
}
}
총알 오브젝트가 생성되면 앞으로 이동하고, 3초 후에 자동으로 파괴되도록 하는 코드를 작성했다.
구현한 화면이다 플레이어는 게임 매니저에서 wasd입력을 받고 플레이어를 움직인다. 탄막은 씬 시작시 앞으로 지정된 방향으로 날아간다.
해결한 문제) 1074번 Z
#include <stdio.h>
#include <math.h>
int sabunmun_z(int n, int row, int col);
void z(int n, int r, int c ,long long int start)
{
if (n == 0)
{
printf("%lld",start);
return;
}
long long int a = start;
long long int half = pow(2, n - 1);
a += half * half * sabunmun_z(n, r, c);
if (r > half) r -= half;
if (c > half) c -= half;
z(n-1, r, c ,a);
}
int sabunmun_z(int n, int row, int col)
{
int k = pow(2, n - 1);
if (k < row)
{
if (k < col)
return 3;
else
return 2;
}
else
{
if (k < col)
return 1;
else
return 0;
}
}
int main(void)
{
int n, r, c;
scanf("%d %d %d", &n, &r, &c);
r++;
c++;
z(n, r, c, 0);
}
n == 0
인 경우, 즉 1x1 크기의 정사각형에 도달한 경우에는 Z 순회의 시작 값을 바로 반환한다. 이때는 더 이상의 분할이 필요하지 않다.(r, c)
가 속하는 사분면을 결정한다. 사분면에 따라 새로운 시작 값과 크기를 갖는 하위 문제로 변환한다.