Making an object orbit using c# script for Unity+Vuforia

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 angleSpeed;

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

}

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

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

• 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. May 28, 2020 at 15:41
• 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. May 28, 2020 at 16:07
• 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. May 28, 2020 at 16:29
• 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. May 28, 2020 at 16:30
• I edited the question. May 28, 2020 at 18:21

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.

• Awesome! it works. May 29, 2020 at 10:51