0
votes

Okay so I am trying to develop a game where A player can choose a character and then said character can move like an RPG on another Scene. I am really struggling with making the Character move and would like help or Advice. The players are UI images set with a tag of Player. Players are in a Empty Game Object named PlayerCanvas under a GameObject child named players. Player trys to move but gets set back to previous position (doesn't move). The Player Creation script is on the child Players along with the Player Movement script.

Player Creation Script:

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);
    }
}

Player Movement script:

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));
        }
    }
}

If there is a way to make the Player move it would be helpful. Cheers.

1
Ugh don't use transform.Translate. Better use the CharacterController, OR a rigidbody and its velocity. It's quite possible, that you are moving your character into the ground and that's why his position gets resettedTobias Theel
So something like this: 'CharacterController controller = GetComponent<CharacterController>();' and 'controller.Move(moveDirection * Time.deltaTime);'sosumi
Yep, but you have to add the CharacterController Component to your object firstTobias Theel
I tried but it didn't achieve the outcome I wanted. It works but depends on what you want to achieve out of your game.sosumi

1 Answers

0
votes

The easiest way is to just recreate it into a Empty without the canvas. You will need to resize all the images you are using besides the ones for the GUI. Don't create players as a GUI.