Unity碰撞体触发交互方法2_天天报道

来源:哔哩哔哩 2021-11-05 12:02

上期我们提到的问题大家还记得吗 ? 物体有了刚体就可以实现碰撞了吗 , 答案是 No... ,它还需要有碰撞体 , 今天就为大家介绍一下碰撞体 :


【资料图】

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using Valve.VR;

public class OpenClip : MonoBehaviour

{

public bool isOpen = false;

public SteamVR_Action_Boolean action;

// Update is called once per frame

void Update()

{

if(SteamVR_Input.GetAction<SteamVR_Action_Boolean>("OpenClip").GetStateDown(SteamVR_Input_Sources.RightHand))

{

GetComponent<Animator>().SetTrigger("open");

isOpen = true;

Debug.Log("ddd");

}

if (SteamVR_Input.GetAction<SteamVR_Action_Boolean>("OpenClip").GetStateUp(SteamVR_Input_Sources.RightHand))

{

GetComponent<Animator>().SetTrigger("close");

isOpen = false;

Debug.Log("uuu");

}

}

}

以上六种碰撞器只是形状不同,属性基本上大致都是相同的 .

Is Trigger : 是否为触发器 ;Material : 碰撞器表面的物理材质 ;Center : 位置 ;Size : 大小 ;

执行代码如下

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PickedItem : MonoBehaviour

{

public GameObject CollidObject;

public GameObject Origin;

public Vector3 PickitemLocalPos = new Vector3(0.0235f,0,0.08f);

public Vector3 PickitemAngles = new Vector3(0, 15.0f, 0);

private void OnTriggerStay(Collider other)

{

if(other.name == CollidObject.name && other.GetComponent<OpenClip>().isOpen==true)

{

transform.SetParent(other.transform);

transform.localPosition = PickitemLocalPos;

transform.eulerAngles = other.transform.eulerAngles + PickitemAngles;

Debug.Log("拾取");

}

}

// Update is called once per frame

void Update()

{

if(CollidObject.GetComponent<OpenClip>().isOpen == false)

{

transform.SetParent(Origin.transform);

transform.position = Origin.transform.position;

transform.rotation = Origin.transform.rotation;

Debug.Log("放下");

}

}

}

关键词:

上一篇: 商丘市教体局:落实落细“管、疏、治”工作要求 为学生撑起心理“保护伞”
下一篇:最后一页
为你精选