아카이브
8/20 주말과제 궁수 이동, 공격 본문
1. 조이스틱으로 플레이어 캐릭터 이동시키기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
private VariableJoystick joystick;
[SerializeField]
private float moveSpeed = 2f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//조이스틱으로 이동
float h = this.joystick.Direction.x;
float v = this.joystick.Direction.y;
Vector3 dir = new Vector3(h, v, 0).normalized;
this.transform.Translate(dir * this.moveSpeed * Time.deltaTime);
}
}
2. 화면 밖으로 나가지 못하도록 막기, walk 애니메이션 추가
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
private VariableJoystick joystick;
[SerializeField]
private float moveSpeed = 2f;
private Animator anim;
// 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 = this.joystick.Direction.x;
float v = this.joystick.Direction.y;
Vector3 dir = new Vector3(h, v, 0).normalized;
this.transform.Translate(dir * this.moveSpeed * Time.deltaTime);
//화면 밖으로 나가지 않게 막기
var clampX = Mathf.Clamp(this.transform.position.x, -2.1f, 2.1f);
var clampY = Mathf.Clamp(this.transform.position.y, -4.2f, 4.2f);
this.transform.position = new Vector3(clampX, clampY, 0);
if (dir == Vector3.zero)
{
//애니메이션
this.anim.SetInteger("State", 0);
}
else
{
//애니메이션
this.anim.SetInteger("State", 1);
}
}
}
3. 화살 발사
PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
private VariableJoystick joystick;
[SerializeField]
private GameObject arrowPrefab;
[SerializeField]
private float moveSpeed = 2f;
private Animator anim;
// 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 = this.joystick.Direction.x;
float v = this.joystick.Direction.y;
Vector3 dir = new Vector3(h, v, 0).normalized;
this.transform.Translate(dir * this.moveSpeed * Time.deltaTime);
//화면 밖으로 나가지 않게 막기
var clampX = Mathf.Clamp(this.transform.position.x, -2.1f, 2.1f);
var clampY = Mathf.Clamp(this.transform.position.y, -4.2f, 4.2f);
this.transform.position = new Vector3(clampX, clampY, 0);
if (dir == Vector3.zero)
{
//애니메이션
this.anim.SetInteger("State", 0);
}
else
{
//애니메이션
this.anim.SetInteger("State", 1);
}
//키보드 누르면 화살 생성
if (Input.GetKeyDown(KeyCode.Space))
{
GameObject arrowGo = Instantiate(this.arrowPrefab);
//화살 생성 위치
Vector3 offset = new Vector3(0, 2f, 0);
arrowGo.transform.position = this.transform.position + offset;
}
}
}
ArrowController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrowController : MonoBehaviour
{
[SerializeField]
public float arrowSpeed = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.Translate(Vector3.up * arrowSpeed * Time.deltaTime);
//y값이 5넘으면 파괴
if (this.transform.position.y > 5f)
{
Destroy(this.gameObject);
}
}
}
'유니티 심화' 카테고리의 다른 글
HeroShooter 적이 플레이어 추적 (0) | 2023.08.29 |
---|---|
Hero Shooter 문열기, 애니메이션 적용 (0) | 2023.08.24 |
Hero Shooter 조이스틱 이동, 포탈생성 (0) | 2023.08.23 |
총알발사 (0) | 2023.08.18 |
캐릭터 이동 (0) | 2023.08.17 |