I am working on a 2D Unity game where you can switch between different characters and moving them around when in control and while not in control they have their own behaviours. For example: I have two characters loli and the other is pop. The main character is loli and you can move her around as you please. But what if you need to move pop somewhere because there is an enemy that she isn't aware of. So, the player clicks on pop, moves her just like you could with loli and you can come back to controlling loli by clicking back to loli. In order to do that I'll need to have each character to read the player control script and follow what it says and they only read it if, loli or pop, was clicked by the player. Note that loli is the default character so when the game starts she's the one being controled by the player unless the player clicks on pop. The thing is I only know how to do this on C, python and lua, not C#. So how do I do that? Do I make a method in the player code and tell the others to reference it?
player's code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class user : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
public int move = 5;
private int acceleration = 2;
public int Acceleration { get => acceleration; set => acceleration = value;
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.W))
{
transform.position = Vector2.up * move * Input.GetAxis("Vertical");
}
if (Input.GetKey(KeyCode.S))
{
transform.position = Vector2.down * move * Input.GetAxis("Vertical");
}
if (Input.GetKey(KeyCode.D))
{
transform.position = Vector2.right * move * Input.GetAxis("Horizontal");
}
if (Input.GetKey(KeyCode.A))
{
transform.position = Vector2.left * move * Input.GetAxis("Horizontal");
}
}
}
Loli's code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class loli : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
/*
* read user.cs. until another character has been selected
* then I'll do my own thing :D
* Until I get clicked on D:
*/
}
}
pop's code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pop : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
/*
* if i have been clicked on,
* read user.cs.
* else, do my own thing :D
*/
}
}