- Home /
How to create a circular motion for an object moving in curved path?
Hi,
I have curved path on which my player is steering through a spline nodes (using cat mull equation). Front face of path is circular and I want my player to make a circular motion on a particular keystroke. For my game scene I have aligned my game objects in following manner:
Spline Nodes
Player Mover which follows spline nodes and steer through a path (Parent)
Player (Child of Player Mover)
Camera(Child of Player and Grand child of Player Mover)
I managed to create a circular motion for a player using sin/cos equation for circle. But when circular motion starts, my player looses its contact with the transformation updates provided by parent object(player mover). It only follows the transformation along z axis and player even starts to phase out of the path when the path became curve.
Please suggest a solution. Thanks in advance.
Using following code to make a circular motion:
speed = (Mathf.PI * 2) / timeToCompleteCircle;
float x;
float y;
switch (direction) {
case "left":
currentAngle += Time.deltaTime * speed;
x = radius * Mathf.Cos (currentAngle);
y = radius * Mathf.Sin (currentAngle);
transform.position = new Vector3 (x, y, transform.position.z);
break;
Answer by Bunny83 · Jul 19, 2017 at 05:00 PM
Do not use the "position " property as it sets the worldspace position of the object. Just use transform.localPosition
instead.
transform.localPosition = new Vector3 (x, y, 0);
Thanks mate! Its working, it means transform.position is for world space?
Yes, transform.position is the objects position in worldspace. When reading that property Unity actually calculates the worldspace position based on the parent's local space and the objects local position. localPosition actually defines the objects location within the local space of the parent object. If an object doesn't have any parent, localPosition and position are the same.
ps: When you assign a Vector3 to transform.position, Unity actually transforms that position into the local space of the parent and set the localPosition. The localPosition is what you see in the inspector when you select an object.
Your answer
