0
좋아, 그래서 플레이어가 캐릭터를 고르고 다른 캐릭터의 RPG처럼 움직일 수있는 게임을 개발하려고합니다. 나는 캐릭터를 움직이는 데 정말로 고심하고 있으며 도움이나 조언을 원합니다. 플레이어는 플레이어의 태그로 설정된 UI 이미지입니다. 플레이어는 플레이어라는 GameObject 하위의 PlayerCanvas라는 빈 게임 개체에 있습니다. 플레이어는 이동하려고 시도하지만 이전 위치로 돌아갑니다 (이동하지 않음). 플레이어 생성 스크립트는 플레이어 이동 스크립트와 함께 하위 플레이어에 있습니다.유니티에서 움직이는 플레이어 UI 이미지
플레이어 생성 스크립트 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterCreation : MonoBehaviour {
private List<GameObject> players;
// Default Index for players
private int selectionIndex = 0;
private void Start()
{
players = new List<GameObject>();
foreach(Transform t in transform)
{
players.Add(t.gameObject);
t.gameObject.SetActive(false);
}
players[selectionIndex].SetActive(true);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
public void Select (int index)
{
if(index == selectionIndex)
{
return;
}
if(index < 0 || index >= players.Count)
{
return;
}
players[selectionIndex].SetActive(false);
selectionIndex = index;
players[selectionIndex].SetActive(true);
}
}
플레이어 운동 스크립트 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
if(Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < 0.5f)
{
transform.Translate(new Vector3 (Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < 0.5f)
{
transform.Translate(new Vector3(Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f, 0f));
}
}
}
플레이어가 도움이 될 것입니다 이동 할 수있는 방법이 있다면. 건배.
transform.Translate를 사용하지 마십시오. 강체와 속도를 더 잘 사용하십시오. 캐릭터를 땅으로 옮기는 것이 가능하기 때문에 자신의 위치가 재설정됩니다. –
'CharacterController controller = GetComponent();' 및 'controller.Move (moveDirection * Time.deltaTime);' –
Lucifer
그래,하지만 먼저 CharacterController Component를 객체에 추가해야한다. –