Unity开关门的脚本,从VRTK的例子里边找的

来源:互联网 发布:淘宝会员号是什么 编辑:程序博客网 时间:2024/06/05 04:33

在做HTC-VR项目时发现了这个脚本,用于开关门,感觉很不错,就稍微改了下(就是把VRTK_SDK相关的删了),自己用,看代码:

using UnityEngine;public class OpenDoor:MonoBehaviour{    public bool flipped = false; //正转还是倒转    public bool rotated = false;//是否相对相机旋转    public float smooth = 60.0f; //旋转速度    public float doorOpenAngle = -90f; //旋转角度        private float sideFlip = -1;    private float side = -1;    private bool open = false; //当前状态    private Vector3 defaultRotation;//初始旋转欧拉    private Vector3 openRotation;//目标旋转欧拉    //触发方法    public void StartUsing()    {        SetDoorRotation(transform.position);        SetRotation();        open = !open;    }    private void Start()    {        defaultRotation = transform.eulerAngles;        SetRotation();        sideFlip = (flipped ? 1 : -1);    }    private void Update()    {        if (open)        {            transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(openRotation), Time.deltaTime * smooth);        }        else        {            transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(defaultRotation), Time.deltaTime * smooth);        }    }    //设置新的旋转    private void SetRotation()    {        openRotation = new Vector3(defaultRotation.x, defaultRotation.y + (doorOpenAngle * (sideFlip * side)), defaultRotation.z);    }    //是否两面旋转,相机在哪人在哪(实验出来的在VR里)    private void SetDoorRotation(Vector3 interacterPosition)    {        side = ((rotated == false && interacterPosition.z > transform.position.z) || (rotated == true && interacterPosition.x > transform.position.x) ? -1 : 1);    }}

想让啥转让啥转,也可以用开关,把这个脚本放到开关物体上,然后将transform改成门的物体就行了,同理可以随便控制物体旋转,如果有人看的话,多给意见



原创粉丝点击