0
だから私はAプレイヤーがキャラクターを選択し、別のシーンのRPGのように動くことができるゲームを開発しようとしています。キャラクターを動かすことに本当に苦労しており、助けや助言が必要です。選手はPlayerのタグで設定されたUI画像です。プレイヤーは、プレイヤーというGameObjectの子の下にPlayerCanvasという空のゲームオブジェクトに入っています。プレイヤーは移動しようとしますが、前の位置に戻ってきます(移動しません)。 Player Creationスクリプトは、Player Movementスクリプトと共に子プレーヤーにあります。ユニティのプレイヤー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、または剛体とその速度をうまく使いましょう。あなたのキャラクターを地面に動かすことは可能です。その理由は、彼の位置がリセットされる理由です。 –
このような感じです: 'CharacterController controller = GetComponent();'と 'controller.Move(moveDirection * Time.deltaTime);' –
Lucifer
けれども、最初にCharacterControllerコンポーネントをオブジェクトに追加する必要があります –