“手摇” 搅拌机

43 篇文章 0 订阅
7 篇文章 0 订阅
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hand_trigger : MonoBehaviour {
    SteamVR_TrackedObject trackobj;
    SteamVR_Controller.Device device;
    public GameObject sphere;
    //把手
    public GameObject jiaobanji;
    //角度变量
    private float number;
    //观测的物体
    public GameObject cube_;


   
    private float timeone;
    private float timetwo;
    private float timejishi;
    //speed
    private float aaa=1;
   
    Quaternion v;
	// Use this for initialization
	void Start () {
        trackobj = transform.GetComponent<SteamVR_TrackedObject>();       
    }
    private void FixedUpdate()
    {
       
    }
    // Update is called once per frame
    void Update () {
        
        try
        {
            device = SteamVR_Controller.Input((int)trackobj.index);
        }
        catch (System.Exception)
        {

            throw;
        }
       
    }
    private void OnTriggerStay(Collider other)
    {
        if (other.name == "gan")
        {
            if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
            {
                //sphere与摇杆轴在一个面上&&当一直接触时 sphere的除面上坐标 其他与手柄坐标一致(我这里是x轴)
                sphere.transform.position = new Vector3(sphere.transform.position.x, transform.position.y, transform.position.z);
                //判断前后y值
                timejishi += Time.deltaTime;
                if (timejishi <0.02f)
                {
                    timeone = sphere.transform.position.y;
                    print("1" + timeone);
                }
                if(timejishi>0.02f&& timejishi<0.04f)
                {
                    timetwo = sphere.transform.position.y;
                    print("2" + timetwo);
                }
                if (timejishi > 0.04f)
                {
                    timejishi -= 0.04f;
                }
               //分界线
                if (sphere.transform.position.z > 0.7)
                {
                    number = 0;
                    
                    if (timetwo - timeone <0f)
                    {

                        //上升;
                        cube_.transform.Translate(Vector3.up * Mathf.Abs(timetwo - timeone) * aaa);
                        //cube_.transform.position =Vector3.Lerp(cube_.transform.position,new Vector3 (1, 1 +Mathf.Abs(timetwo - timeone) * aaa, 0.5f),1*Time.deltaTime);
                        print("上升");

                    }
                    else
                    {
                        //下降;
                        cube_.transform.Translate(Vector3.down * Mathf.Abs(timetwo - timeone) * aaa);
                        //cube_.transform.position = Vector3.Lerp(cube_.transform.position, new Vector3(1, 1 - Mathf.Abs(timetwo - timeone) * aaa, 0.5f), 1 * Time.deltaTime);
                        print("下降");
                    }
                }
                //分界线
                if (sphere.transform.position.z <= 0.7)
                {
                    number = -1;
                    if (timetwo - timeone < 0)
                    {
                        //下降;
                        cube_.transform.Translate(Vector3.down * Mathf.Abs(timetwo - timeone) * aaa);
                        //cube_.transform.position = Vector3.Lerp(cube_.transform.position, new Vector3(1, 1 - Mathf.Abs(timetwo - timeone) * aaa, 0.5f), 1 * Time.deltaTime);
                        print("下降");
                    }
                    else
                    {

                        //上升;
                        cube_.transform.Translate(Vector3.up * Mathf.Abs(timetwo - timeone) * aaa);
                        //cube_.transform.position = Vector3.Lerp(cube_.transform.position, new Vector3(1, 1 + Mathf.Abs(timetwo - timeone) * aaa, 0.5f), 1 * Time.deltaTime);
                        print("上升");
                    }
                }
               
                    
                
                print("cha值"+Mathf.Abs(timetwo - timeone));
                //核心 z轴朝向sphere 
                other.transform.LookAt(sphere.transform);
                //角度自测在y轴会出现自旋转 需手动调角度 number为z轴变换角度变量
                other.transform.localEulerAngles = new Vector3(other.transform.localEulerAngles.x, other.transform.localEulerAngles.y,180*number);
            }
            if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
            {
              
                timejishi = 0f;
            }
           
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值