- Home /
GameObject movement to specific position and angle
Hello, I am trying to build a visualization tool for a vehicles that moves into a layout and that communicates to the script his X Y (Z for unity) and angle of direction.
The scripts receive through a WCF service the updates position of the vehicle in the layout and make the gameObject move to that position.
I am very new to unity but I know a little of C# programming.
I am facing the following issues:
The vehicle when aproaches a turn, it curves in the wrong direction and it seems to be drifting on the ground.
I can't manage to have a smooth rotation like it is for the translation.
If I increase the step of the translation it does not animate anymore but teleports to the target point.
here what my code looks like:
private Vector3 target;
private int counter = 0;
public AgvCtlServicesClient AgvCtlSvcs;
// Start is called before the first frame update
void Start()
{
AgvCtlSvcs = new AgvCtlServicesClient(new BasicHttpBinding(), new EndpointAddress("http://localhost:9001/AgvCtlSvcs"));
var coords = AgvCtlSvcs.GetAgvCoordinates();
transform.position = new Vector3((((float)coords[0]) / 1000), 2, ((float)coords[1] / 1000));
}
// Update is called once per frame
void FixedUpdate()
{
counter++;
if (counter % 1 == 0)
{
float step = (float) 1f * Time.deltaTime;
var coords = AgvCtlSvcs.GetAgvCoordinates();
target = new Vector3((((float)coords[0]) / 1000), 2, ((float)coords[1] / 1000));
float temp = Vector3.Distance(transform.position, target);
transform.position = Vector3.MoveTowards(transform.position, target, step);
var angle = -(transform.eulerAngles.y - (float)((coords[2] * 360) / 6284) / 10);
gameObject.transform.Rotate(new Vector3(0, angle, 0));
print("X: " + ((coords[0]) / 1000).ToString() + " Y: " + (coords[1] / 1000).ToString() );
}
}
what do you think I should correct? it's not commented but it is pretty simple code I think.