- Home /
Question by
Eagullfly · Aug 22, 2020 at 09:14 PM ·
scripting beginnerrts
How to possess a character?
Hi, I'm new to Unity and I'm trying to make a project that has some RTS like features. However, I also want to give the player the ability to possess a character they summon. So that they can move the character around and perform actions themselves. I'd appreciate it if someone could help me out.
Comment
Answer by highpockets · Aug 22, 2020 at 09:13 PM
Your character controller script could be on an empty GameObject and you could have a currentCharacter variable on the controller which you could set to whatever character you want to control:
using UnityEngine;
public class CharacterController : MonoBehaviour
{
[SerializeField] private GameObject _currentCharacter;
public GameObject currentCharacter
{
//use a setter to set your character
set{ _currentCharacter = value; }
}
private void Update
{
//when you do your controller logic point to _currentCharacter, this is just an example without logic to move the character forward 1 meter per second
_currentCharacter.transform.position = _currentCharacter.transform.position + _currentCharacter.transform.forward * Time.deltaTime;
}
}
That’s should get you started