- Home /
Scripting Help(C#): Make object face a direction based on rotation of another
I am new to unity and I was chosen to do the scripting part for a "group" project. I don't have the words to explain the problem so I hope this illustration will help.
What I basically want to happen is to make the little plane rotate in the direction that you would expect it to when the earth is rotating
This part is not very urgent since we were only tasked to make a prototype but this is pretty much the only part missing. Any help will be appreciated
Note: The earth is the one that rotates, the camera and airplane NEVER moves
EDIT: I probably should've added that the user DOES NOT CONTROL the rotation. I am making some kind of trivia game.
Answer by mtdrume · Aug 19, 2015 at 10:10 AM
UPDATED:
Add a cube to the scene and position it in front of your plane. Disable the mesh renderer. Make the cube a child of the planet. Create a C# script named Look and attach it to the plane. Look Script:
using UnityEngine;
using System.Collections;
public class Look: MonoBehaviour {
public GameObject other;
void Update()
{
transform.LookAt(Vector3(other.transform.position.x, transform.position.y, other.transform.position.z));
}
}
Select the Plane object and drag the cube object over to the script at other.
The plane will now only rotate on the Y axis, looking at the cube that is rotating with the planet.
@adrian4545 fixed my answer for you to do what you want. Tried and tested ;)
I can definitely use this, just need to figure out a way to move the cube. Thanks
attaching the cube to the planet will have it rotate with it. you could also make an empty gameobject and center it to the planet and give it its own rotational path!
@adrian4545 If you think this answered your question, please mark as correct :)