- Home /
How to move character from one room to another?
Hi, I created a one level house and a character. I would like to move the character from one room to another without clicking or using buttons. In this project the character should change his position, based on information that comes from some sensors. But for this stage all I want is to trigger the character to move from a specific room to another when I start the game.
Answer by jackmw94 · Nov 15, 2020 at 11:51 AM
You could make a pretty simple door component to move you from area to area?
public class Door : MonoBehaviour
{
[SerializeField] private Door targetDoor;
private void OnTriggerEnter(Collider collider)
{
// this check has to determine whether the object is a player
// ideally this would be if it has some players specific component
// but could also be based on tag
if (collider.gameObject.GetComponent<YourPlayerController>())
{
targetDoor.ArriveAtDoor(collider.transform);
}
}
private void ArriveAtDoor(Transform arrived)
{
arrived.position = transform.position;
arrived.rotation = transform.rotation;
}
}
Door objects would require a box collider on with isTrigger checked, expand the collider bounds as this is the area that the player has to be in to get transported. Then just drag in another door to use at the target :)
Alternatively if the transition to another room is happening via some logic not based on player position then this could be renamed room. The ArrivedAt function could be public, you can remove the OnTriggerEnter function and then call this via a reference to the next room object at the point at which you determine you want to change areas.
Let me know if I’ve misunderstood what you’re trying to do or whether there’s any of this you don’t understand!
Your answer
Follow this Question
Related Questions
How to make object follow touch position?Pls Help 0 Answers
Access GameObject position and model position in surface shader? 0 Answers
How Can I Create a Button that Takes Multiple Objects and cycles them through a grid? (Unity 2D) 0 Answers
How to control an object with a mouse? 1 Answer
pivot of CharacterController`s collider is not the pivot of the charcter???? 0 Answers