Question

I’m not doing just one player character I’m doing three and they don’t move independently if I move them all three player characters move t the same time, how can i make them move independently?

First, please don’t title your posts; ‘Question’. We (I) scan through the titles to see if we could possibly answer a question and ‘Question’ doesn’t give the info we need to know this. You don’t need to ask the whole question in the title, just mention what your post is about. Like this one could have been ‘How do I implement multiple characters in the obstacle course?’. Now, onto the question

You haven’t specified why you want three characters, so here is what I could think of:

1. Three players on different machines

If you have the three characters so that three people can play at the same time, you would probably have to do the multiplayer course.


2. Three players on the same machine/keyboard

If you want three people to play on the same keyboard you would need a separate Mover for each character, and they will not be looking at Horizontal and Vertical anymore. You’d define keys (like WASD for one, IJKL for another and arrows for the third) for each character.

Quick example:

// In Mover.cs
[SerializeField] KeyCode upKey;
[SerializeField] KeyCode downKey;
[SerializeField] KeyCode leftKey;
[SerializeField] KeyCode rightKey;

private float GetHorizontalAxis()
{
    float result = 0f;
    if (Input.GetKey(leftKey)) result += -1f;
    if (Input.GetKey(rightKey)) result += 1f;
    return result;
}
private float GetVerticalAxis()
{
    float result = 0f;
    if (Input.GetKey(upKey)) result += 1f;
    if (Input.GetKey(downKey)) result += -1f;
    return result;
}

void MovePlayer()
{
    float xValue = GetorizontalAxis() * Time.deltaTime * moveSpeed;
    float zValue = GetVerticalAxis() * Time.deltaTime * moveSpeed;
    transform.Translate(xValue,0,zValue);
}

This is just a quick example. You’d probably benefit more from creating some scriptable object with a ‘key config’ that you can configure and drag onto each character. It’s still simple 'cos we’re just moving the code into a different class

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Obstacle Course / Keyboard Config")
public class KeyboardConfigSO : ScriptableObject
{
    [SerializeField] KeyCode upKey;
    [SerializeField] KeyCode downKey;
    [SerializeField] KeyCode leftKey;
    [SerializeField] KeyCode rightKey;

    public float GetHorizontalAxis()
    {
        float result = 0f;
        if (Input.GetKey(leftKey)) result += -1f;
        if (Input.GetKey(rightKey)) result += 1f;
        return result;
    }
    public float GetVerticalAxis()
    {
        float result = 0f;
        if (Input.GetKey(upKey)) result += 1f;
        if (Input.GetKey(downKey)) result += -1f;
        return result;
    }
}

Now the Mover will be much simpler

// In Mover.cs
[SerializeField] KeyboardConfigSO keyConfig;

void MovePlayer()
{
    float xValue = keyConfig.GetorizontalAxis() * Time.deltaTime * moveSpeed;
    float zValue = keyConfig.GetVerticalAxis() * Time.deltaTime * moveSpeed;
    transform.Translate(xValue,0,zValue);
}

3. One player playing with all 3 characters

If you want one player to play with all three characters, you will need to have a ‘player manager’ type class that will manage which character goes when. This is a simple class that may listen to, say, the number keys 1, 2 and 3 and set the relevant character active and the other characters inactive (only the mover).

Quick example:

// New CharacterSelector.cs script - put it on a new, empty game object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterSelector : MonoBehaviour
{
    [SerializeField] int maxCharacters = 3;
    [SerializeField] Mover[] characters;

    private int activeCharacter;

    void Start()
    {
        SetActiveCharacter(0);
    }

    void Update() 
    {
        // check if we pressed any of the number keys from 1 to maxCharacters (3)
        for (var i = 0; i < maxCharacters; i++)
        {
            KeyCode key = (KeyCode)(49 + i);
            if (Input.GetKeyDown(key))
            {
                SetActiveCharacter(i);
                break;
            }
        }
    }
    
    private void SetActiveCharacter(int index)
    {
        activeCharacter = index;
        for (int i = 0; i < maxCharacters; i++)
        {
            // Enable active character and disable the other character (only the mover)
            characters[i].enabled = (activeCharacter == i);
        }
    }
}

or, to cycle through the characters with, say, the Tab key

// New CharacterSelector.cs script - put it on a new, empty game object
 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterSelector : MonoBehaviour
{
    [SerializeField] int maxCharacters = 3;
    [SerializeField] Mover[] characters;

    private int activeCharacter;

    void Start()
    {
        SetActiveCharacter(0);
    }

    void Update() 
    {
        // check if we pressed tab and cycle through the characters
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            CycleActiveCharacter();
        }
    }
    
    private void CycleActiveCharacter()
    {
        activeCharacter = (activeCharacter + 1) % maxCharacters;
        for (int i = 0; i < maxCharacters; i++)
        {
            // Enable active character and disable the other character (only the mover)
            characters[i].enabled = (activeCharacter == i);
        }
    }
}

This will cycle through each character 1, 2, 3, 1, 2, 3, etc. each time you press the Tab key


4. Three players taking turns

I initially confused the Obstacle Course project with the Delivery Driver project, so the above is mostly aimed at what you’d do with that. If you want to take turns, you won’t have 3 characters on the screen so it’s probably not what you want. But to do that, you’d have a ‘turn manager’ that would keep track of who’s turn it is but it’s really only for things like score. The rest of the game would be exactly the same as the course. When one player fails, the turn manager would change who’s turn it is and then just play the game again.

1 Like

#3 is wht im looking for. the basis of my puzzle game is 3 peas getting back to there pod doing that will obviously rther puzzling haha the title of the game is “3 pes in a pod”.

also would i have to make a script for each character?

No, it shouldn’t be necessary. If the Mover on the 2 inactive peas are disabled they won’t respond. Only the active pea will be moving. The CharacterSelector I posted above should be sufficient to help you switch between the 3 peas.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms