So, I took the Unity 101 “Intro to Unity” online training class with Little Angel (excellent course!) and in it, one of the things we did was to make a controllable sphere roll around in a sort of sandbox.
After the class, I wanted to experiment and add a headlight that follows the direction the “player” (the rolling ball) is moving. I quickly found out that it’s not a very straightforward thing to do, since the ball itself is rotating all over the place. I added a Spotlight light and copied a bit of script (from our camera controller that follows the up/down/left/right coordinates of the player) into a new “Headlight” script (JS) so that the headlight is always “attached to” (in the same location as) the player ball, but it was always pointing “up” (north). After many failed guesses, I got somewhere close to where I wanted to be from slightly modifying a bit of the code from the Euler Angles scripting reference page.
Here’s where I’m at with that script so far: (pastebin).
Either the left/right works or the up/down works right now (depending on which is commented out). Ideally though, I’d want the headlight to face the exact direction the ball is traveling, and not just the four cardinal directions.
Thing is… I know this gets into math territory that I simply don’t know yet. So while a script fix would be cool, I think what I’d really love is recommendations for online (i.e. free) resources for beginner 3d math for games – tutorials or course lessons that have genuinely helped you in the past! (Ideally, with examples that you can probably code or experiment with in Unity along the way.) Thanks for any help or suggestions!
Hey, this will be a really quick answer, but I hope it might cover your problem:
What you can do, is add a Rigidbody to the headlight (which is a child of the sphere). Then you position it accordingly (e.g. it points whatever direction is the forward direction).
Then in the inspector, click the headlight, go to the Rigidbody component and open up “Constraints”. There you have to Freeze the Y rotation, I suppose. I am not 100% sure if this works, but it should. If it doesn’t, then you have to make sure the headlight is not a child in the game editor, but you have to fix the position of the headlight using a script. I hope that the first solution works though!
Thank you for the suggestion (and I tried experimenting with it), but I don’t think it’s that simple (I really wish it was! ) – unfortunately, there is no “forward” on the player ball. To move the ball around, we were using rigidbody.AddForce, so the ball actually rolls around like a real ball would – its local x/y/z could be pointing anywhere. :-\
If you’re using a rigidbody, this is a pretty simple problem:
var headlightDirection = rigidbody.velocity.normalized; //this vector will point along the line you want your headlight to be on - so just multiply it by whatever to get the correct position, like so:
var headlightPosition = transform.position + headlightDirection * ballRadius;
Unfortunately, that didn’t work either. I think I may not have phrased the question properly enough to let people know exactly what I was trying to do.
Good news though – after more reading, and the realization that I had indeed learned this much math long ago (all the Unity references mentioning Vectors and Quaternions were making me think I needed something else), I figured it out. Simple and easy and the headlight now smoothly follows the direction the ball is traveling at all times.
var player : GameObject;
function FixedUpdate () {
transform.position = player.transform.position;
yRotation = Mathf.Rad2Deg * Mathf.Atan2(player.rigidbody.velocity.x,player.rigidbody.velocity.z) + Input.GetAxis("Horizontal");
transform.eulerAngles = Vector3(0, yRotation, 0);
}