L
L
Lev Ozherelyev2018-12-10 23:56:35
C++ / C#
Lev Ozherelyev, 2018-12-10 23:56:35

How to make a character enter the trigger, the player presses a button on the keyboard and this character becomes a child object of another object?

It is necessary that the Persian, when he enters the cube, which is a trigger, the player presses a button on the keyboard and the Persian takes control of the ship.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yura Berezovsky, 2018-12-11
@Jurajkeee

https://docs.unity3d.com/ScriptReference/Collider.... - when the object is in the trigger
https://docs.unity3d.com/ScriptReference/Transform... - we assign the parent
The code will look something like this if I understood you correctly:

//imaginary variables
    public Transform previousParent;
    public GameObject ship;
    public GameObject player;

    bool isNearWheel;
    private void Update()
    {
        if (Input.GetKey(KeyCode.F) && isNearWheel)
        player.transform.SetParent(ship.transform);
        
    }
    private void OnTriggerStay(Collider other)
    {
        if (other.tag == "wheel")
        isNearWheel = true;
        else isNearWheel = false;
    }

Of course, the code is not finalized and I have not tested it, but I hope it will help you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question