与不同的对撞器标签接触时统一更改物理垫

问题描述 投票:0回答:1

我是编码新手,需要帮助。我试图制作一个涉及蹦床的小型游戏,我使用脚本将角色用力发送,但我不希望角色在蹦床上时正常弹跳,因为它们可能会变高,但仍然在正常的非蹦床地面上弹跳。我设置了2个物理垫,我只是不知道如何切换它们。在不同的地方时

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PhysMatChange : MonoBehaviour
{
    void OnCollisionEnter(Collision collisionInfo)
    {
        Debug.Log(collisionInfo.collider.name);
        if (collisionInfo.collider.tag == "trampup") ;

    }
}
c# unity3d
1个回答
0
投票

更改对撞机col的物理材料:

https://docs.unity3d.com/ScriptReference/Collider.html

https://docs.unity3d.com/ScriptReference/Collider-material.html

https://docs.unity3d.com/ScriptReference/PhysicMaterial.html

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PhysMatChange : MonoBehaviour
{
    public PhysicsMaterial materialOne; 
    public PhysicsMaterial materialTwo; 
    private Collider col;

    private void Start()
    {
        col = GetComponent<Collider>(); 
    }

    void OnCollisionEnter(Collision collisionInfo)
    {
        Debug.Log(collisionInfo.collider.name);
        if (collisionInfo.collider.tag == "trampup")
        {
            // change to the first material
            col.material = materialOne; 
        }
        else if (collisionInfo.collider.tag == "trampdown")
        {
            // change to second material
            col.material = materialTwo; 
        }
    }
}

您可以在检查器中配置物理材料的反射度

© www.soinside.com 2019 - 2024. All rights reserved.