0
\$\begingroup\$

I have written a simple c# script for Unity+Vuforia. I want to create a simple sample project where an object like a sphere is revolving (orbiting) origin (0,0,0) above image target.

The code I have written is here

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

public class Orbit : MonoBehaviour
{
    float angles;
    float radiuss;
    float angleSpeed;

    // Start is called before the first frame update
    void Start()
    {
        angles = 0;
        radiuss = 0.2f;
        angleSpeed = 1;

    }

    // Update is called once per frame
    void Update()
    {
        angles += Time.deltaTime * angleSpeed;


        float x = radiuss * Mathf.Cos(Mathf.Deg2Rad * angles);
        float z = radiuss * Mathf.Sin(Mathf.Deg2Rad * angles);
        float y = 0;

        transform.position = new Vector3(x, y, z);
    }
}

It works perfectly fine if I am not using it with Vuforia, but when I attach this script as component to an object in Vuforia, it does not work and acts weird. These are the configurations and hierarchy enter image description here

enter image description here

Please help me out, I tried a lot of things.

\$\endgroup\$
  • 1
    \$\begingroup\$ What are the specific symptoms of "does not work and acts weird" that you observe? "I tried a lot of things" - what specific things? It's important to tell us, lest we give you answers that just repeat things you already tried. Try showing us how you've configured this script on (a child of?) your Viewforia object, so we can see exactly how your scenario is set up. \$\endgroup\$ – DMGregory May 28 at 15:41
  • \$\begingroup\$ As can be seen from the code I am keeping the value of y constant at 0, but y does not remain constant and keeps on increasing. Also I am unsure if the values of x and z are going according to the script as they are changing very fast. I tried reducing the speed, added Using Vuforia (which I dont think is needed), but it is not solving the problem. I am new to Vuforia. \$\endgroup\$ – Chetan Waghela May 28 at 16:07
  • \$\begingroup\$ I checked the coordinates again on the inspector, the values of x,y,z are correct until I do not show the targetimage. Once, the targetimage is detected, the problem starts. \$\endgroup\$ – Chetan Waghela May 28 at 16:29
  • 1
    \$\begingroup\$ Sounds like your Viewforia positioning script might be fighting with this one for control of the same object. Did you mean to put this script on a child object, positioned relative to the parent object that Viewforia is positioning to match the image marker? As I asked before, please show us how these scripts are configured in your object hierarchy and inspector views, so we understand your scenario completely. \$\endgroup\$ – DMGregory May 28 at 16:30
  • \$\begingroup\$ I edited the question. \$\endgroup\$ – Chetan Waghela May 28 at 18:21
1
\$\begingroup\$

It looks to me like you want to change this line:

transform.position = new Vector3(x, y, z);

to use localPosition instead:

transform.localPosition = new Vector3(x, y, z);

This says "place me here relative to my parent's coordinate system," so as Vuforia moves the parent around, this object will track that motion, while also layering its own motion on top.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Awesome! it works. \$\endgroup\$ – Chetan Waghela May 29 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.