11-22-2023, 09:51 PM
hello
im trying to make a game like rush(https://play.google.com/store/apps/details?id=com.ketchapp.rush&hl=en_US)
but i have problem in implementing curvy road
my problem is that road have bad rotation (look at image)
https://img.onl/oSsD
it happens some times not all time
and this is image of my game(image)
https://img.onl/TQb96w
and this is how i insert segments
public void start_game()
{
mDir = Vector3.forward;
last_x = 0;
last_y = 0;
for (int i = 0; i < cp_count_to_start; i++)
{
Vector3 p = TrackSpline.ControlPointsList[TrackSpline.ControlPointCount - 1].transform.localPosition;
Vector3 position = TrackSpline.transform.localToWorldMatrix.MultiplyPoint3x4(p + mDir * cp_step_size);
// UnityEngine.Random.value => it float between 0 and 1
//DTUtility.RandomSign() => its float between -1 and 1
float rndX = UnityEngine.Random.value * CurvationX * DTUtility.RandomSign();
float rndY = UnityEngine.Random.value * CurvationY * DTUtility.RandomSign();
mDir = Quaternion.Euler(rndX, rndY, 0) * mDir;
CurvySplineSegment newControlPoint = TrackSpline.InsertAfter(null, position, true);
}
TrackSpline.Refresh();
for (int i = 8; i < TrackSpline.ControlPointsList.Count; i++)
{
enemy g = Instantiate(enemy_prefab, TrackSpline.ControlPointsList[i].transform.position, Quaternion.identity).GetComponent<enemy>();
g.gameObject.transform.rotation = Quaternion.LookRotation(TrackSpline.GetTangent(TrackSpline.ControlPointsList[i].TF));
enemys.Add(g.gameObject);
}
Controller.Speed = 100;
Controller.Play();
}
public void Track_OnControlPointReached(CurvySplineMoveEventArgs e)
{
if (e.ControlPoint.Distance > 10 * cp_step_size)
{
tranfer_to_end(enemys[0]);
GameObject g = enemys[0];
enemys.RemoveAt(0);
enemys.Add(g);
}
}
public void tranfer_to_end(GameObject g)
{
float pos = Controller.AbsolutePosition;
pos -= TrackSpline.ControlPointsList[0].Length;
TrackSpline.Delete(TrackSpline.ControlPointsList[0], true);
Vector3 p = TrackSpline.ControlPointsList[TrackSpline.ControlPointCount - 1].transform.localPosition;
Vector3 position = TrackSpline.transform.localToWorldMatrix.MultiplyPoint3x4(p + mDir * cp_step_size);
float rndX = UnityEngine.Random.value * CurvationX * DTUtility.RandomSign();
float rndY = UnityEngine.Random.value * CurvationY * DTUtility.RandomSign();
mDir = Quaternion.Euler(rndX, rndY, 0) * mDir;
CurvySplineSegment newControlPoint = TrackSpline.InsertAfter(null, position, true);
TrackSpline.Refresh();
Controller.AbsolutePosition = pos;
g.transform.position = newControlPoint.transform.position;
g.transform.rotation = Quaternion.LookRotation(TrackSpline.GetTangentFast(newControlPoint.TF));
}
PLZ HELP
im trying to make a game like rush(https://play.google.com/store/apps/details?id=com.ketchapp.rush&hl=en_US)
but i have problem in implementing curvy road
my problem is that road have bad rotation (look at image)
https://img.onl/oSsD
it happens some times not all time
and this is image of my game(image)
https://img.onl/TQb96w
and this is how i insert segments
public void start_game()
{
mDir = Vector3.forward;
last_x = 0;
last_y = 0;
for (int i = 0; i < cp_count_to_start; i++)
{
Vector3 p = TrackSpline.ControlPointsList[TrackSpline.ControlPointCount - 1].transform.localPosition;
Vector3 position = TrackSpline.transform.localToWorldMatrix.MultiplyPoint3x4(p + mDir * cp_step_size);
// UnityEngine.Random.value => it float between 0 and 1
//DTUtility.RandomSign() => its float between -1 and 1
float rndX = UnityEngine.Random.value * CurvationX * DTUtility.RandomSign();
float rndY = UnityEngine.Random.value * CurvationY * DTUtility.RandomSign();
mDir = Quaternion.Euler(rndX, rndY, 0) * mDir;
CurvySplineSegment newControlPoint = TrackSpline.InsertAfter(null, position, true);
}
TrackSpline.Refresh();
for (int i = 8; i < TrackSpline.ControlPointsList.Count; i++)
{
enemy g = Instantiate(enemy_prefab, TrackSpline.ControlPointsList[i].transform.position, Quaternion.identity).GetComponent<enemy>();
g.gameObject.transform.rotation = Quaternion.LookRotation(TrackSpline.GetTangent(TrackSpline.ControlPointsList[i].TF));
enemys.Add(g.gameObject);
}
Controller.Speed = 100;
Controller.Play();
}
public void Track_OnControlPointReached(CurvySplineMoveEventArgs e)
{
if (e.ControlPoint.Distance > 10 * cp_step_size)
{
tranfer_to_end(enemys[0]);
GameObject g = enemys[0];
enemys.RemoveAt(0);
enemys.Add(g);
}
}
public void tranfer_to_end(GameObject g)
{
float pos = Controller.AbsolutePosition;
pos -= TrackSpline.ControlPointsList[0].Length;
TrackSpline.Delete(TrackSpline.ControlPointsList[0], true);
Vector3 p = TrackSpline.ControlPointsList[TrackSpline.ControlPointCount - 1].transform.localPosition;
Vector3 position = TrackSpline.transform.localToWorldMatrix.MultiplyPoint3x4(p + mDir * cp_step_size);
float rndX = UnityEngine.Random.value * CurvationX * DTUtility.RandomSign();
float rndY = UnityEngine.Random.value * CurvationY * DTUtility.RandomSign();
mDir = Quaternion.Euler(rndX, rndY, 0) * mDir;
CurvySplineSegment newControlPoint = TrackSpline.InsertAfter(null, position, true);
TrackSpline.Refresh();
Controller.AbsolutePosition = pos;
g.transform.position = newControlPoint.transform.position;
g.transform.rotation = Quaternion.LookRotation(TrackSpline.GetTangentFast(newControlPoint.TF));
}
PLZ HELP