- Home /
How do I rotate a Gameobject in the direction of its motion?
I have a car as a Gameobject, which moves according to the GPS of an Android phone. Now, I want it to face its direction of motion, without using the gyroscope of the location provider, i.e. the Android phone.
To do the same, I have created another Gameobject which also moves according to the GPS. And then I inserted this script in my Car Gameobject. This is so that the car will LookAt the direction of motion of the other Gameobject, which actually is moving in its own direction.
using UnityEngine;
using System.Collections;
public class Rotation_car : MonoBehaviour {
public GameObject Object2;
void Start ()
{
Object2= GameObject.Find("game object's name");
}
void Update ()
{
transform.LookAt(Object2.transform.position);
}
}
But when I hit the Play button in Unity, the place where the 'Object2' is selected in the script attached to the 'Car' gets unselected and it says 'None'.
I am not sure if this is the reason the script is not working or not. I wanted a solution to it. Or if you have any other solution to rotate a Gameobject in the direction of its own motion, or another Gameobject's motion, that would help a lot. Thank you.
If you attached Object2 via the inspector, you don't need that line in Start(). It's none probably, because there is no object with name you passed in the parameter.
Answer by hectorux · Oct 29, 2018 at 04:10 PM
Instead of Find, is better to work with tags. Also you could create a virtual transform and apply to it the script you have to Object2. If the resoult is null, it maybe because the given name is wrong
Could you please elaborate? I guess the 'None' issue is solved by @Casiell 's solution. But could you provide a solution to rotate a Gameobject so that it faces its direction of motion?
Your answer
Follow this Question
Related Questions
Slerp object in direction of traveling 1 Answer
How to rotate object to cursor? 1 Answer
how to change orbit direction when button clicked? 0 Answers
Tarnsfer direction 2 Answers