PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
private float moveSpeed = 3f;
private Animator anim;
[SerializeField]
private BulletController bulletController;
// Start is called before the first frame update
void Start()
{
this.anim = this.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//움직이고 화면가두기
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector2 moveDir = (Vector2.up * v) + (Vector2.right * h);
this.transform.Translate(moveDir.normalized * this.moveSpeed * Time.deltaTime);
float clampX = Mathf.Clamp(this.transform.position.x, -2.30f, 2.30f);
float clampY = Mathf.Clamp(this.transform.position.y, -4.50f, 4.50f);
this.transform.position = new Vector2(clampX, clampY);
//왼쪽 방향키 누르면 애니메이션 왼쪽 실행
if(Input.GetKey(KeyCode.LeftArrow))
{
this.anim.SetInteger("State", 1);
}
//오른쪽 방향키 누르면 애니메이션 오른쪽 실행
else if (Input.GetKey(KeyCode.RightArrow))
{
this.anim.SetInteger("State", 2);
}
//아무것도 아니라면 애니메이션 센터 실행
else
{
this.anim.SetInteger("State", 0);
}
if(Input.GetKeyDown(KeyCode.Space))
{
//총알 소환
bulletController.Fire();
}
}
}
BulletController
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class BulletController : MonoBehaviour
{
[SerializeField]
private float force = 3f;
private GameObject targetTr;
// Start is called before the first frame update
void Start()
{
this.targetTr = this.GetComponent<GameObject>();
this.targetTr = GameObject.Find("Player");
this.transform.position = this.targetTr.transform.position + this.targetTr.transform.up * 0.5f;
}
private void Update()
{
this.transform.Translate(Vector2.up * this.force * Time.deltaTime);
if(this.transform.position.y >= 4.85f)
{
Destroy(this.gameObject);
}
}
public void Fire()
{
Instantiate(this.gameObject);
}
}
'유니티 심화' 카테고리의 다른 글
절대강좌 유니티 - 총알 이펙트효과 (0) | 2023.08.21 |
---|---|
[주말과제] - 궁수의 전설[완] (0) | 2023.08.19 |
챕터5 - 총알 (0) | 2023.08.18 |
Ref (0) | 2023.08.18 |
Lerp, Slerp (0) | 2023.08.18 |