- Home /
How to rotate a game object in a given path?
Hello, guys. I have a game object that will follow the path given to move. But now i face a problem, how can i add some rotate coding in the pathfollower coding? What kind of rotate coding that i can use? This is the pathfollower coding: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PathFollower : MonoBehaviour {
public Transform[] path;
public float speed = 5.0f;
public float reachDist = 0.5f;
public int currentPoint = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Vector3 dir = path [currentPoint].position - transform.position;
float dist = Vector3.Distance(path[currentPoint].position, transform.position);
transform.position = Vector3.MoveTowards (transform.position, path[currentPoint].position, Time.deltaTime * speed);
if ( dist <= reachDist )
{
currentPoint++;
}
if ( currentPoint >= path.Length)
{
currentPoint = 0;
}
}
void OnDrawGizmos ()
{
for ( int i = 0; i< path.Length; i++)
{
if (path[i] != null)
{
Gizmos.DrawSphere(path[i].position, reachDist);
}
}
}
}
Thanks all.
I don't understand the question. If you want to rotate the object you can do this by ransform.Rotate
for example. You can find a few suitable ways to do this here:
https://docs.unity3d.com/ScriptReference/Transform.html
Following the path should not affect rotation, since you will not change object's position.
Answer by FernandoHC · Aug 16, 2018 at 11:18 AM
I've added the transform.lookAt to your code, seems like the easiest way to get the job done. However, I would strongly suggest using a tweener, like DOTween, based on the type of actions you are doing in your game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathFollower : MonoBehaviour
{
public Transform[] path;
public float speed = 5.0f;
public float reachDist = 0.5f;
public int currentPoint = 0;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//Vector3 dir = path [currentPoint].position - transform.position;
float dist = Vector3.Distance(path[currentPoint].position, transform.position);
transform.position = Vector3.MoveTowards(transform.position, path[currentPoint].position, Time.deltaTime * speed);
transform.LookAt(path[currentPoint].position);
if (dist <= reachDist)
{
currentPoint++;
}
if (currentPoint >= path.Length)
{
currentPoint = 0;
}
}
void OnDrawGizmos()
{
for (int i = 0; i < path.Length; i++)
{
if (path[i] != null)
{
Gizmos.DrawSphere(path[i].position, reachDist);
}
}
}
}
Thanks bro for your helping!!!! This coding can work perfectly in my game!
Answer by TheKnightsofUnity · Aug 16, 2018 at 11:05 AM
Hello @KeyinPhua.
You can set the rotation of an object simply by adding this line of code:
transform.rotation = Quaternion.LookRotation(path[currentPoint].position - transform.position);
If you would like to lock Y axis of the rotation. You can use it like this:
Vector3 rotationVector = path[currentPoint].position - transform.position;
rotationVector.y = 0;
transform.rotation = Quaternion.LookRotation(rotationVector);
This method is simply rotating object into vector direction.
All the best,
The Knights of Unity
Thanks a lot for your helping!!! This coding can work! Thanks bro!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
problem after rotate an object 1 Answer
Controlling the rotation speed of an object? 2 Answers
How to make UI screen space image to look at game object in world space 0 Answers