Hello everyone,
I started using Unity a week ago for a university project. Right now, I am a bit stuck on an issue and I could not find an existing topic that solved it.
In our lab, we have a small room equipped with cameras and a system to get the coordinates (translation + rotation) of a moving object (a drone) inside the room. Then we use ROS (http://www.ros.org/) to publish this information in real time (with a frequency of 100 Hz) in our wifi network. So with my computer connected to the network, I can receive and use the flow of data (positions of the drone) directly in Unity.
Basically, what I want to do is move an object smoothly in Unity scene according to the movement of the drone using this data flow.
Until now, I haven’t been successful : the object follow the drone’s movement but it is very laggy with a lot of jittering…
Here is what I tried :
First I initialize the connection in the Start method : after the subscribe method is called, the callback method OnMessage will be called each time we receive data from the network.
Subscriber<TransformStamped> sub;
NodeHandle nh;
float x, y, z;
static readonly object _locker = new object();
UnityEngine.Vector3 positions;
//public float smoothTime = 0.3F;
//private UnityEngine.Vector3 velocity = UnityEngine.Vector3.zero;
// Use this for initialization
void Start()
{
x= 0;
y = 0;
z = 0;
positions = new UnityEngine.Vector3(0,0,0);
ROS.Init(new string[0], "example_listener");
nh = new NodeHandle();
sub = nh.subscribe<TransformStamped>("/vicon/Quad7/Quad7", 1, onMessage);
}
private void onMessage(TransformStamped tr)
{
lock (_locker)
{
x = -(float)tr.transform.translation.y;
y = (float)tr.transform.translation.z;
z = (float)tr.transform.translation.x;
}
}
Then I use the update method to change the position of the object at each frame :
void Update()
{
lock (_locker)
{
positions.x = x;
positions.y = y;
positions.z = z;
//UnityEngine.Vector3 targetPosition = new UnityEngine.Vector3(x, y, z);
//transform.position = UnityEngine.Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
//transform.position = UnityEngine.Vector3.Lerp(transform.position, positions, Time.deltaTime);
transform.position = positions;
}
}
I thought it would help to put a lock in the callback method OnMessage and in the Update method, because I didn’t want x,y,z to be modified while I was updating the position, and maybe the lock slows down the Update method and that’s bad.
Also I tried different method to try and make the movement between the successive position smoother, but I didn’t succeed, it didn’t change anything.
I am a beginner in Unity, so I am pretty sure I did a lot of mistakes/bad practice…
Thank you for the help,
Cyril