The Platform is moving along the waypoints. in the screenshot of the platform axis, the Red looks like facing forward the Blue facing down and the Green is facing to the left.
The capsule on each platform is kind of npc or player and his axis looks something else :
I'm not sure what part I should rotate by logic if the platform or the capsule. in this case, I think the capsule should be rotating facing the next waypoint each time. The rotation should be while the transform is started moving.
This script is attached to each Platform. and in lines 77 and 78 I'm doing the rotation but for now I'm rotating the transform that's the Platform and not the Capsule so I should add a public variable to get a reference to each capsule. but anyway the rotation is not working I tried to run the game and the Platform is not rotating at all. but I think that I should rotate the capsule not the platform but not sure how to do it.
This is the lines 77-78 part :
transform.rotation = Quaternion.RotateTowards(transform.rotation,
Quaternion.LookRotation(waypoints.lineRendererPositions[index]), Time.time * lookAtRotationSpeed);
The full script :
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class WaypointsFollower : MonoBehaviour
{
public float speed;
public Waypoints waypoints;
public bool go;
public bool goForward;
public float lookAtRotationSpeed;
private int index = 0;
private int counter = 0;
private int c = 0;
private List<GameObject> curvedLinePoints = new List<GameObject>();
public int numofposbetweenpoints;
private bool getonce;
private bool getBackwardIndexOnce = true;
private void Start()
{
waypoints = GameObject.Find("Waypoints").GetComponent<Waypoints>();
curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();
if(waypoints.moveInReverse == false)
{
goForward = true;
}
else
{
goForward = false;
}
if(goForward)
{
index = 0;
}
}
private void Update()
{
if (getonce == false)
{
numofposbetweenpoints = curvedLinePoints.Count;
getonce = true;
}
if (go == true && waypoints.lineRendererPositions.Count > 0)
{
if(goForward == false && getBackwardIndexOnce)
{
index = waypoints.lineRendererPositions.Count - 1;
getBackwardIndexOnce = false;
}
Move();
}
}
private void Move()
{
Vector3 newPos = transform.position;
float distanceToTravel = speed * Time.deltaTime;
bool stillTraveling = true;
while (stillTraveling)
{
Vector3 oldPos = newPos;
transform.rotation = Quaternion.RotateTowards(transform.rotation,
Quaternion.LookRotation(waypoints.lineRendererPositions[index]), Time.time * lookAtRotationSpeed);
// error exception out of bound on line 55 to check !!!!!
newPos = Vector3.MoveTowards(oldPos, waypoints.lineRendererPositions[index], distanceToTravel);
distanceToTravel -= Vector3.Distance(newPos, oldPos);
if (newPos == waypoints.lineRendererPositions[index]) // Vector3 comparison is approximate so this is ok
{
// when you hit a waypoint:
if (goForward)
{
bool atLastOne = index >= waypoints.lineRendererPositions.Count - 1;
if (!atLastOne)
{
index++;
counter++;
if (counter == numofposbetweenpoints)
{
c++;
counter = 0;
}
if (c == curvedLinePoints.Count - 1)
{
c = 0;
}
}
else { index--; goForward = false; }
}
else
{ // going backwards:
bool atFirstOne = index <= 0;
if (!atFirstOne)
{
index--;
counter++;
if (counter == numofposbetweenpoints)
{
c++;
counter = 0;
}
if (c == curvedLinePoints.Count - 1)
{
c = 0;
}
}
else { index++; goForward = true; }
}
}
else
{
stillTraveling = false;
}
}
transform.position = newPos;
}
}
Time.time * lookAtRotationSpeed
- sure that you don't meanTime.deltaTime
here? – Philipp May 20 '21 at 12:24Quaternion.LookRotation
needs a direction vector, not a world position. You can get a direction vector by subtracting the current position from the position you want to look at. – Philipp May 20 '21 at 12:26