0
Follow
0
View

unity on-off character hiding and component disabled

lingyun8201 注册会员
2023-02-27 21:23

This answer quotes ChatGPT

To do this, you need to write code in the game engine. Here are some general ideas, which can vary depending on the game engine and programming language you are using.

First, you need to create a character object and a vehicle object. When the player presses the "F" key, detect if the character has collided with the vehicle, if so, set the character's position to the vehicle position, and hide the character object, enable the vehicle component, and disable the character component. At this point, the player can control the vehicle.

When the player presses the "F" key again, detects whether the vehicle currently controlled by the player has collided with another vehicle, if so, sets the player's position to the vehicle position, displays the character object, disables the vehicle component, and enables the character component.

Here is an example of a C# script implementation based on the Unity game engine, assuming that characters and vehicles are assigned tags of "Player" and "Car", respectively:


public class CarController : MonoBehaviour
{
    public GameObject player;
    public GameObject car;

    private bool isDriving = false;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            if (!isDriving && Vector3.Distance(player.transform.position, car.transform.position) < 2.0f)
            {
                isDriving = true;
                player.SetActive(false);
                car.GetComponent().enabled = true;
                car.GetComponent().enabled = false;
            }
            else if (isDriving && Vector3.Distance(player.transform.position, car.transform.position) < 2.0f)
            {
                isDriving = false;
                player.SetActive(true);
                car.GetComponent().enabled = false;
                car.GetComponent().enabled = true;
            }
        }
    }
}

About the Author

Question Info

Publish Time
2023-02-27 21:23
Update Time
2023-02-27 21:23

Related Question

Unity忽然出现for educational use only

Unity无法构建WebGL项目

Unity 3d 报错Object reference not set to an instance of an object

Unity3D学习路线

Unity打包之后ios 卡顿 跳帧

Unity2D敌人刚体粘在玩家身上

Unity UDP 中文乱码

请问 Unity Zspace 怎么实现触控笔拖动物体移动 或者物体跟随触控笔移动?

unity如何实现灯停电几秒有恢复有停电这样的效果

Unity 3D OnCollisionEnter