- Home /
Problem is not reproducible or outdated
How do I change between characters mid game? Similair to Lego games.
Hello, I'm something of a beginner in Unity and I'm trying to make a mechanic where the player can take control of objects in a scene, preferably at the press of a button (Space) I've looked up tutorials online but a lot of them are using scripts with elements I'm not yet using, such as animators etc. My scene is currently just made up of basic objects, so I can test the mechanic out first. If someone could help out a beginner like me, I'd love you 3000.
you are lost my friend.
Go look that tutorials. Try to learn what you can first, dont try to get what you want i f are not ready to see things you dont understand... Thats learning.
Fair point! I've actually made some progress since I posted this, and am able to change between two characters when pressing a button! Now I just need to work out enabling it in a certain proximity.. Thanks for the advice!
You may wanna post your solution as answer to your question then (or remove the question), so that others are helped too (or not try to answer it). Good luck!
An Update: Found the answer. This script also includes my movement, so just bear that in $$anonymous$$d. Add a sphere collider to every object you want to become playable, and then add this script to it. $$anonymous$$ake sure the script is unticked, but the bool "selected" is ticked. Oh and of course make sure all playable objects have Player tag. Basically this scripts detects those objects, and when the space button is pressed, the player will switch to a detected object.
[SerializeField] float moveSpeed = 4f;
Vector3 forward, right;
public bool selected;
Rigidbody Rb;
public GameObject detectedObject;
void Start()
{
forward = Camera.main.transform.forward;
forward.y = 0;
forward = Vector3.Normalize(forward);
right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
Rb = gameObject.GetComponent<Rigidbody>();
}
void Update()
{
if (selected && detectedObject != null) //take object in trigger and enable its Player$$anonymous$$ove script and disable current script
{
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Space))
{
detectedObject.GetComponent<Player$$anonymous$$ove>().enabled = true;
this.enabled = false;
}
}
}
void FixedUpdate()
{
if (selected)
{
Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Vector3 right$$anonymous$$ovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
Vector3 up$$anonymous$$ovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("Vertical");
Vector3 heading = Vector3.Normalize(right$$anonymous$$ovement + up$$anonymous$$ovement);
if ($$anonymous$$athf.Abs(Input.GetAxis("Horizontal")) > 0.1f)
{
transform.forward = heading;
}
if ($$anonymous$$athf.Abs(Input.GetAxis("Vertical")) > 0.1f)
{
transform.forward = heading;
}
transform.position += right$$anonymous$$ovement;
transform.position += up$$anonymous$$ovement;
}
}
void OnTriggerStay(Collider other) //get the playable object and hold it in a variable
{
if (other.tag == "Player" && other.gameObject.name != gameObject.name)
{
detectedObject = other.gameObject;
}
}
void OnTriggerExit(Collider other) //empty the variable if the object is no longer in trigger
{
if (other.tag == "Player" && other.gameObject.name == detectedObject.name)
{
detectedObject = null;
}
}
}
Follow this Question
Related Questions
Why is my character controller not working? 1 Answer
Top-Down Movement in Unity 1 Answer
8-Axis 3D Top Down Movement 1 Answer
How to Logically Match Ground Slope While Using This Code? 1 Answer
Movement with AddForce: Wrong Direction 2 Answers