简体   繁体   中英

Clamping an object axis in Vuforia AR camera

screenshot here I want to clamp Y-axis on a cube. I can do it in Unity camera. But, it does not react correctly when I am using it in Vuforia camera. My problem was that the cube follows the camera. I would like the cube to stay in its position and ignore the AR camera position. I sense it has something to do with WorldtoViewpoint but I cannot figure it out. Can you teach me how to do this please? thankyou

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

public class ClampMovementController : MonoBehaviour
{


    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        Vector3 pos = transform.localPosition;
        pos.y = Mathf.Clamp(transform.position.y, 0f, 0f);
        transform.localPosition = pos;
    }

}  

This is my solution: Actually its very simple. The INcorrect concept was my object attached to the AR camera, hence, object position is always moving related to camera position. Now. In order to make the object stays in its place. I need to get its localPosition. First. Store the localposition in Vector3 pos. And then do modification on Vector3 pos. At last, reassign the new value to the object localposition.

public class ClampMovementController : MonoBehaviour
{
    public float currentPos;
    public GameObject capsule;

    void Update()
    {
        //store the value of object localPosition
        Vector3 pos = capsule.transform.localPosition;
        //modification on the value
        pos.y = Mathf.Clamp(pos.y, currentPos, currentPos);
        //rerassign the new value to the object localPosition
        capsule.transform.localPosition = pos;
    }

}

First of all your cube is moving with the camera because your image target is child of your ARCamera. Therefore, when you move the camera image target moves, then your cube moves as well. Make sure your ImageTarget has no parent.

I did not understand why you have to lock any movement in Y axis. I guess you are doing something wrong with lean touch when you move object. I have not used lean touch but i have achieved this with keyboard inputs. You can convert it to lean touch by modifying following script. Just add these line to your ImageTarget 's DefaultTrackableEventHandler script:

//Variables for getting capsule and checking if ImageTarget is tracked
private bool isTracked = false;
private GameObject capsule;

Then create an Update method for getting input from user like this.

 void Update()
{
    if(isTracked)
    {
        if(Input.GetKey(KeyCode.W))
        {
            //using forward for moving object in z axis only. 
            //Also using local position since you need movement to be relative to image target
            //Global forward can be very different depending on your World Center Mode
            capsule.transform.localPosition += Vector3.forward * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            capsule.transform.localPosition -= Vector3.forward * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.A))
        {
            //Using Vector3.left and right to be make sure movement is in X axis.
            capsule.transform.localPosition += Vector3.left * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.D))
        {
            capsule.transform.localPosition += Vector3.right * Time.deltaTime;
        }
    }
}

As you can see there is no movement in Y axis because i used forward, left and right vectors to make sure movement in in only X and Y axis.

Last you have to make sure isTracked is updated. In order to do that you have to add isTracked = false; in OnTrackingLost method and isTracked = true; in OnTrackingFound method. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM