using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Transform tr;
public float moveSpeed = 10.0f;
private Animator anim;
[SerializeField]
private Bullet bullet;
// Start is called before the first frame update
void Start()
{
tr = GetComponent<Transform>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Move();
if (Input.GetKeyDown(KeyCode.Space))
{
Bullet bl = Instantiate(bullet);
bl.transform.position = this.transform.position;
}
}
private void Move()
{
//카메라뷰로 이동제한
Vector3 worldPos = Camera.main.WorldToViewportPoint(this.transform.position);
if (worldPos.x < 0.1f) worldPos.x = 0.1f;
if (worldPos.x > 0.9f) worldPos.x = 0.9f;
if (worldPos.y < 0.08f) worldPos.y = 0.08f;
if (worldPos.y > 0.92f) worldPos.y = 0.92f;
this.transform.position = Camera.main.ViewportToWorldPoint(worldPos);
//이동
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector2 moveDir = new Vector2(h, v);
Vector2 dir = moveDir.normalized;
tr.Translate(dir * moveSpeed * Time.deltaTime);
if (h > 0)
{
this.anim.SetInteger("State", 2);
}
else if (h < 0)
{
this.anim.SetInteger("State", 1);
}
else
{
this.anim.SetInteger("State", 0);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
private float moveSpeed = 20.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.Translate(Vector2.up * moveSpeed * Time.deltaTime);
Vector3 worldPos = Camera.main.WorldToViewportPoint(this.transform.position);
if (worldPos.y > 1f)
{
Destroy(this.gameObject);
}
}
}
'유니티 심화' 카테고리의 다른 글
유니티 new Input System으로 조이스틱 사용하기 (0) | 2023.08.31 |
---|---|
가장 가까운 무기 장비하기 + 배럴 공격하기 (0) | 2023.08.21 |
플레이어 충돌 시 법선벡터(normal)방향으로 회전 (0) | 2023.08.21 |
궁수의 전설 (0) | 2023.08.20 |
유니티 카메라 줌인 + 회전 따라가기 (0) | 2023.08.17 |