How to move object in 45/30/15 degree arch.
My GameObject ( say a car) is move along in some direction and some point it need to move in from p1 to p2, (the red spot in picture, each element) in and arch like fashion and not just straight. Like a turn 45 degree turn in the road, (the game is in 3d)
.
I only know where to road piece starts and ends and if it 45/30 and so forth.
So fare i have only been apple to move straight. I use Vector.Lerp.
t += Time.deltaTime / timeToMove; transform.position = Vector3.Lerp(startPos, endPos, t); transform.rotation = Quaternion.Lerp(transform.rotation, rot, t);
Answer by unity_68Qt_Q-7Cs99dQ · Mar 14, 2019 at 02:50 PM
I managed to find a way to calculate the origin of the arc that i wanted my little car to follow. I then used a while loop around (code snip) inside an IEnumerator method.
t += Time.deltaTime / timeToMove;
transform.RotateAround(getCenter(startPos, endPos, e.Direction),
Vector3.up, (float) (turnDegreeFloat * Math.PI / 180) * t);
The getCenter is as follows:
Vector3 getCenter(Vector3 startPos, Vector3 endPos, String direction)
{
Vector3 vectorBetweenPoints = endPos - startPos;
float LengthBetweenPoints = vectorBetweenPoints.magnitude;
Vector3 centerBetweenPoints = new Vector3((startPos.x + endPos.x) /
2, (startPos.y + endPos.y) / 2, (startPos.z + endPos.z) / 2);
float centerX = 0;
float centerZ = 0;
if (direction == "RIGHT")
{
centerX = (float)(centerBetweenPoints.x - ((Math.Sqrt(Math.Pow(CurveRadius, 2) -
Math.Pow(LengthBetweenPoints / 2, 2)) * (startPos.z - endPos.z)) / LengthBetweenPoints));
centerZ = (float)(centerBetweenPoints.z - ((Math.Sqrt(Math.Pow(CurveRadius, 2) -
Math.Pow(LengthBetweenPoints / 2, 2)) * (endPos.x - startPos.x)) / LengthBetweenPoints));
}
else
{
centerX = (float)(centerBetweenPoints.x + ((Math.Sqrt(Math.Pow(CurveRadius, 2) -
Math.Pow(LengthBetweenPoints / 2, 2)) * (startPos.z - endPos.z)) / LengthBetweenPoints));
centerZ = (float)(centerBetweenPoints.z + ((Math.Sqrt(Math.Pow(CurveRadius, 2) -
Math.Pow(LengthBetweenPoints / 2, 2)) * (endPos.x - startPos.x)) / LengthBetweenPoints));
}
Vector3 vector = new Vector3(centerX, startPos.y, centerZ);
return vector;
}
Answer by WarmedxMints · Mar 12, 2019 at 07:32 PM
You could create a curve along each piece and then get a point on that curve for your position and angle. The code to move it could be done better but it should serve as a starting point for you.
using UnityEngine;
public class RotateBetweenVectors : MonoBehaviour
{
//These could just be vectors rather than transforms if you wish.
public Transform StartPoint;
public Transform Midpoint;
public Transform Endpoint;
public Transform ObjectToMove;
public float MoveSpeed = 0.01f;
private float _currentPointAlongCurve;
//The amount of points along the curve in the scene view
public int CurveResolution = 16;
public void OnDrawGizmos()
{
if (StartPoint == null || Midpoint == null || Endpoint == null)
return;
var percent = 1f / CurveResolution;
Gizmos.color = Color.blue;
for (var i = percent; i < 1+ percent; i += percent)
{
Gizmos.DrawLine(GetPointOnCurve(StartPoint.position, Midpoint.position, Endpoint.position, i - percent),
GetPointOnCurve(StartPoint.position, Midpoint.position, Endpoint.position, i));
}
}
public void Update()
{
if (ObjectToMove == null)
return;
_currentPointAlongCurve += Time.deltaTime * MoveSpeed;
if (_currentPointAlongCurve > 1)
_currentPointAlongCurve = 0;
ObjectToMove.transform.position = GetPointOnCurve(StartPoint.position, Midpoint.position, Endpoint.position, _currentPointAlongCurve);
ObjectToMove.transform.rotation = Quaternion.LookRotation(GetPointOnCurve(StartPoint.position, Midpoint.position, Endpoint.position,
_currentPointAlongCurve + 0.01f) - GetPointOnCurve(StartPoint.position, Midpoint.position, Endpoint.position, _currentPointAlongCurve), Vector3.up);
}
public Vector3 GetPointOnCurve(Vector3 p0, Vector3 p1, Vector3 p2, float t)
{
return Vector3.LerpUnclamped(Vector3.LerpUnclamped(p0, p1, t), Vector3.LerpUnclamped(p1, p2, t), t);
}
}
Thank you very much for you answer. I really appreciate it. The problem is that i dont have the luxery of setting a midpoint in the editor. Actually, what is happening in the game is that i read an xml file, which tell me where to put element, and there type. The from and event log file i read where and element is in my system. Ex. car with id 3 is at element of type curve 45, or straight. each element has startPoint and EndPoint, which is what i used to move across the straight elements.
I found that actually i know the curves center radius to be 2.5. Any idea how i calculate a midpoint with this infomation.