How Can i change Player to another player temporary

Hi guys

  • i’m creating a Platform game using CORGI engine ! and i wanted the main player to change to another player with different abilities “whenever it’s touch a specific prefab” for like 10 seconds and return to main player again.

  • so what i need is : i need script (C#) i can drag it on Main Player Prefab and on inspector i can drag the second player and set the amount of time and select that specific prefab "that player need to come across to activate that script " .

i hope you got my idea
i’ve learned java and now i moved to unity i found out that it’s work with javascript and c#
so please if anyone can help i’ll appreciate it

Basically you need two playable characters that have an on/off switch for processing inputs. This could be by disabling the components, or some coded boolean, etc.

Then all you have to do is turn one on, and turn the other one off, making sure to update which player your camera is following if necessary.

This can be done a thousand different ways, some more extensible and maintainable than others.

A strong design to do this is having the user represented by a Controller class, which then can Possess and Unpossess one or many characters in the game. You can imagine the scenario of a character running around, then getting into a car. The PlayerController remains the same, but the player is unpossessed, and the car is possessed. Then the car begins updating and receiving inputs until you press the button to get out, which is when the player becomes possessed again, and the car becomes unpossessed.

Using that design, you can have things like the MainCamera automatically follow the PlayerController’s currently possessed character, and other convenient functions.

Here’s an example of one way to implement something like that using a simple Interface:

// defines a contract that objects can agree to
// objects with this interface are guaranteed to contain the defined functions
public interface IPossessable
{
    void Possess();
    void UnPossess();
}
using UnityEngine;

// controller that handles possessing and unpossessing
// operates only on objects that implement the IPossessable interface
public class Controller : MonoBehaviour
{
    public IPossessable controlledObject;

    public void Possess(IPossessable obj)
    {
        if(obj != null)
        {
            // unpossess the current object
            if(controlledObject != null)
            {
                controlledObject.UnPossess();
            }
            // possess the new object
            controlledObject = obj;
            controlledObject.Possess();
        }
    }

    public void UnPossess()
    {
        // unpossess the current object
        if(controlledObject != null)
        {
            controlledObject.UnPossess();
        }
    }
}
using UnityEngine;

// example of a character that implements the interface
public class Character : MonoBehaviour, IPossessable
{
    public float speed = 10;

    // required by the interface
    void IPossessable.Possess()
    {
        enabled = true; // turning on the component makes Update start ticking
    }

    // required by the interface
    void IPossessable.UnPossess()
    {
        enabled = false; // turning off the component makes Update stop ticking
    }

    // just some example behavior
    private void Update()
    {
        // follows pointer while the component is enabled
        transform.position = Vector3.MoveTowards(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), speed * Time.deltaTime);
    }
}

Thank you so much jeffreyschoch
Now should only apply that script to Main character right ?

If you use an interface like that, then you can use any object. As long as they implement the interface to toggle their controls on and off, then you can use the Controller to possess them.

The scripts above are just a bare-bones example of how to set something like that up. You will still need to reference the Controller, and to call Possess on the object you want to control.

If you have trouble with this setup, there’s another easy way to toggle controls, which is simply to set a boolean. It’s not a great solution for the long-term, but it’s easy to code and will get the job done.

bool canMove = true;

private void Update()
{
    if(canMove)
    {
        // process inputs and move
    }
}

then you would just set “canMove” to false on the object you want to stop moving.

If you want to use the Controller setup, I’m happy to help you get that set up, but for a small game there’s nothing wrong with using booleans for this.