unity与Kinect开发之3d物体跟随人物指定关节匹配移动

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


//Kinect跟随的实现
//如何准确让物体跟随图像的手
//在场景新建一个空物体名为KinectManager,
//挂在组件KinectManager 修改Compute User Map为BodyTexture,勾选Compute Color Map
//挂在组件BackgroundRemovalManager  设置ForegroundCamera为主相机,取消勾选ColorCameraResolution
//新建一个材质,修改shader为Unlit/transparent
//设置参数为  Tiling  x 1.5     Y -1
//            Offset  X -0.25   Y  1
//新建一个canvas  修改RenderMode 为  ScreenSpace-Camera ,然后挂载此组件
//新建一个RawImage  将新建的材质赋给他,


public class CanvasHandTrack : MonoBehaviour {
    [SerializeField] private Camera camera;//必须是主相机
    [SerializeField] private RawImage humanImage;
    private float width = 1920f;
    private float high = 1080f;

    private float depthWidth = 512f;
//Debug.Log("11" + mBackgroundRemovalManager.GetForegroundTex().width);
    private float depthHigh = 424f; 
//Debug.Log("11" + mBackgroundRemovalManager.GetForegroundTex().height);
    //512/424=1.207   需要保持图片的比例一致

    private KinectManager kinectManager;
    private long userID;
    private Rect rect = new Rect(307.92f, 1080f, 1304.15f, -1080f);
   //转换的目标尺寸
            //1304.15/1080=1.207
                      //(x:-960.00, y:-540.00, width:1920.00, height:1080.00)
    private BackgroundRemovalManager mBackgroundRemovalManager;
    private void Awake()
    {
        //固定参数很重要
        rect = new Rect(Screen.width * 0.16f, Screen.height, Screen.width * 0.67924f, -Screen.height);
    }
    // Use this for initialization
    void Start () {
        kinectManager = KinectManager.Instance;
        mBackgroundRemovalManager = BackgroundRemovalManager.Instance;
    }
	
	// Update is called once per frame
	void Update () {
        if (kinectManager.IsInitialized())
        {
            SetHand(kinectManager.GetUserIdByIndex(0), hand1, KinectInterop.JointType.HandRight, out rightHandCameraPos1st);
            SetHand(kinectManager.GetUserIdByIndex(0), hand2, KinectInterop.JointType.HandLeft, out lefthandCameraPos1st);

            SetHumanImage();
        }
    }
    [SerializeField] private Transform hand1;
    [SerializeField] private Transform hand2;
    private Vector3 rightHandCameraPos1st;
    private Vector3 lefthandCameraPos1st;
    private Vector3 rightHandCameraPos2nd;
    private Vector3 lefthandCameraPos2nd;private void SetHand(long userID, Transform hand, KinectInterop.JointType jointType, out Vector3 cameraPos)
    {
        Vector3 point;
        point = kinectManager.GetJointPosDepthOverlay(userID, (int)jointType, camera, rect);
        point = camera.WorldToScreenPoint(point);//世界坐标转成屏幕坐标
        point.z = 0;//固定z值
        point.y = Screen.height - point.y;//翻转Y值
        cameraPos = point;
        point.z = transform.position.z;
        point = camera.ScreenToWorldPoint(point);//屏幕坐标转成世界坐标
        point.z = transform.position.z;
        hand.position = point;
    }

    //获取人物图片
    private void SetHumanImage()
    {
        humanImage.texture = mBackgroundRemovalManager.GetForegroundTex();

    }
}

如何移动图片之后仍然跟随移动

//移动控制图片的时候需要将手的匹配位置也要移动
public class WorldHandController : MonoBehaviour
{
    [SerializeField] private Transform rightHand1st;
    [SerializeField] private Transform leftHand1st;
    [SerializeField] private Transform CucumberFactoryTrans;
    [SerializeField] private CanvasHandTrack canvasHandTrack;
    [SerializeField] private Transform humanImageTrans;
    private Camera mCamera;
    private float initY;
    void Start()
    {
        mCamera = Camera.main;
        initY = humanImageTrans.position.y;
    }
    //此脚本的作用就是固定手的Z值
    void Update()
    {
        Vector3 right1stTempPoint = canvasHandTrack.RightHandCameraPos1st;
        Vector3 left1stTempPoint = canvasHandTrack.LefthandCameraPos1st;   

        //固定Z值平面
        left1stTempPoint.z = right1stTempPoint.z
            = CucumberFactoryTrans.position.z;

        right1stTempPoint = mCamera.ScreenToWorldPoint(right1stTempPoint);
        left1stTempPoint = mCamera.ScreenToWorldPoint(left1stTempPoint);
     
        //固定Z值平面
        left1stTempPoint.z = right1stTempPoint.z
            = CucumberFactoryTrans.position.z;

        //在移动人物图片上下的时候,保持手的Y值跟随没问题
        right1stTempPoint.y -= (initY - humanImageTrans.position.y);
        //Debug.Log(" right1stTempPoint.y:"+(initY - humanImageTrans.position.y));
        left1stTempPoint.y -= (initY - humanImageTrans.position.y);
        //Debug.Log("  left1stTempPoint.y" + (initY - humanImageTrans.position.y));

        rightHand1st.position = right1stTempPoint;
        leftHand1st.position = left1stTempPoint;
    }
}

看不到请联系我qq1186908998

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要实现鼠标控制角色移动,可以按照以下步骤进行: 1. 获取鼠标在屏幕上的位置。可以使用Input.mousePosition来获取鼠标位置信息。 2. 将鼠标位置转换为世界坐标系中的位置。可以使用Camera.ScreenToWorldPoint将屏幕坐标转化为世界坐标。 3. 计算人物朝向,可以使用Quaternion.LookRotation方法来计算人物的朝向。 4. 让人物朝向鼠标所在的位置,可以使用Transform.rotation属性来设置人物的旋转。 5. 让人物向鼠标所在的位置移动,可以使用Transform.Translate方法来移动人物。 下面是示例代码,可以放在Update函数中实现人物跟随鼠标移动: ```csharp void Update() { // 获取鼠标在屏幕上的位置 Vector3 mousePos = Input.mousePosition; // 将鼠标位置转换为世界坐标系中的位置 Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, transform.position.y - Camera.main.transform.position.y)); // 计算人物朝向 Quaternion rotation = Quaternion.LookRotation(worldPos - transform.position); // 让人物朝向鼠标所在的位置 transform.rotation = rotation; // 让人物向鼠标所在的位置移动 transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed); } ``` 其中,moveSpeed为人物移动速度,可以自行调整。 ### 回答2: 在Unity3D中,要实现人物动画跟随鼠标移动的效果,可以按照以下步骤进行操作: 1. 创建一个3D人物模型并添加动画组件,确保人物模型已经设置好基本的动画动作。 2. 创建一个脚本,并将其附加到人物模型上。这个脚本将负责处理鼠标移动相关的逻辑。 3. 在脚本中,首先需要获取鼠标的屏幕坐标。可以使用Input类提供的鼠标相关的方法来获取。 4. 接下来,将鼠标的屏幕坐标转换为世界坐标。可以使用Camera类提供的ScreenToWorldPoint方法来完成转换。 5. 然后,将人物的位置设置为鼠标的世界坐标。可以使用Transform组件的position属性来设置。 6. 最后,根据人物移动方向和速度,播放相应的移动动画。可以使用Animator组件来控制人物的动画播放。 需要注意的是,以上只是基本的实现思路,具体的代码细节还需要根据项目的需求进行相应的调整。另外,为了使人物移动更加平滑,可以使用插值算法来处理人物的位置更新。 ### 回答3: 在Unity3D中,要实现人物动画跟随鼠标移动,可以按照以下步骤进行操作: 1. 首先,先确保人物模型已经导入到场景中,并且已经添加好动画控制器。 2. 在脚本中,在Update()函数中获取鼠标的位置。可以使用Input类的鼠标输入函数来获取鼠标的X和Y坐标。 3. 将鼠标的X和Y坐标转换为世界坐标系中的位置。可以使用Camera类的ScreenToWorldPoint()函数,将屏幕上的坐标转换为世界坐标。 4. 接下来,需要将人物的朝向设置为鼠标的位置。可以使用Quaternion.LookRotation()函数来计算人物的朝向。 5. 将计算出的朝向应用到人物模型上,可以通过设置人物模型的transform.rotation属性实现。 6. 最后,根据人物的朝向,选择合适的动画状态进行播放。可以在动画控制器中设置不同朝向下对应的动画状态,并根据计算出的朝向来切换动画状态。 这样,在每一帧更新的时候,就能够根据鼠标的位置进行人物的朝向和动画状态的切换,实现人物动画跟随鼠标移动的效果。 需要注意的是,以上仅为实现基本的鼠标跟随移动和动画切换的逻辑,具体的实现方式可能有所不同,可以根据具体的场景需求和人物模型的动画设置进行调整和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值