# unity – How can I make the transform to rotate facing the moving direction the transform move?

The goal is to make the transform to rotate smooth when the moving direction is change.

In this case there are some cubes and the cubes facing direction the blue axis is not the same as the transform move. The transform is moving up then back down but never rotate because he is facing the cubes directions. but I want that the transform will rotate facing the direction he is moving so on the start he should rotate first time facing up then at the end he should rotate facing down.

not when the transform get to the last waypoint but when he change direction.

``````using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class MoveOnCurvedLines : MonoBehaviour
{
public LineRenderer lineRenderer;
public float speed;
public bool go = false;
public bool moveToFirstPositionOnStart = false;
public float rotSpeed;
public bool random = false;
public int currentCurvedLinePointIndex;

private Vector3() positions;
private Vector3() pos;
private int index = 0;
private bool goForward = true;
private List<GameObject> curvedLinePoints = new List<GameObject>();
private int numofposbetweenpoints;
private bool getPositions = false;
int randomIndex;
int curvedPointsIndex;

// Start is called before the first frame update
void Start()
{
curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();

if (curvedLinePoints != null && curvedLinePoints.Count > 0)
{
transform.rotation = curvedLinePoints(1).transform.rotation;
}

if (random)
GetNewRandomIndex();
}

Vector3() GetLinePointsInWorldSpace()
{
positions = new Vector3(lineRenderer.positionCount);
//Get the positions which are shown in the inspector
lineRenderer.GetPositions(positions);

//the points returned are in world space
return positions;
}

// Update is called once per frame
void Update()
{
if (lineRenderer.positionCount > 0 && getPositions == false)
{
pos = GetLinePointsInWorldSpace();
numofposbetweenpoints = pos.Length / curvedLinePoints.Count;

if (moveToFirstPositionOnStart == true)
{
transform.position = pos(index);
}

getPositions = true;
}

if (go == true && lineRenderer.positionCount > 0)
{
Move();

transform.localRotation = Quaternion.RotateTowards(transform.localRotation, curvedLinePoints(c).transform.localRotation, Time.deltaTime * rotSpeed);
}

var dist = Vector3.Distance(transform.position, curvedLinePoints(curvedPointsIndex).transform.position);
if (dist < 0.1f)
{
if (curvedPointsIndex < curvedLinePoints.Count - 1)
curvedPointsIndex++;

currentCurvedLinePointIndex = curvedPointsIndex;
}
}

int counter = 0;
int c = 1;
void Move()
{
Vector3 newPos = transform.position;
float distanceToTravel = speed * Time.deltaTime;

bool stillTraveling = true;
while (stillTraveling)
{
Vector3 oldPos = newPos;

newPos = Vector3.MoveTowards(oldPos, pos(index), distanceToTravel);

distanceToTravel -= Vector3.Distance(newPos, oldPos);
if (newPos == pos(index)) // Vector3 comparison is approximate so this is ok
{
// when you hit a waypoint:
if (goForward)
{
bool atLastOne = index >= pos.Length - 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;
}

void GetNewRandomIndex()
{
randomIndex = UnityEngine.Random.Range(0, curvedLinePoints.Count);
}
}
``````

This is the line for the rotation that should make it rotating to the moving direction but it’s not.

``````transform.localRotation = Quaternion.RotateTowards(transform.localRotation, curvedLinePoints(c).transform.localRotation, Time.deltaTime * rotSpeed);
``````

In this screenshot the platform is moving up but facing to the left like the other cubes.
and I want the transform(platform) to rotate to be facing to the moving direction. I tried this instead in the rotation part :

``````Vector3 relativePos = curvedLinePoints(c).transform.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
transform.rotation = rotation;
``````

but now when it’s moving up it’s facing up but then when moving down in the middle it’s changing the facing direction to up again still moving down but facing up.

The curvedLinePoints is List of the cubes.

Posted on