[Solved]Make Object face correct direction

Hi,

I’m sure this is elementary for the wizards reading this, but I’ve searched and searched and I just can’t implement it with my set up. I have a 2D game, with an isometric camera. I have a cube with only 1 side textured facing the camera. The cube is hit (its a golf ball) and flies off in a random direction depending on the wind strength - but it always faces up, even if the ball is curving off to the west, so I wanted it to check the direction it was flying in and face that and update each frame during flight.

This is my code so far:

#pragma strict

internal var ballHeight : float;
var power : float = 100.0;
var height : float = 80.0;
var windx : float;
var windy : float;
var windSpeed : float;


function Update () {

	if (Input.GetButtonUp("Fire1")){
		hitball();
		
	}	
	
rigidbody.AddForce(Vector3.right * windSpeed, ForceMode.Acceleration);

//change size of sprite on height
ballHeight = transform.position.z;
transform.localScale = Vector3(ballHeight / 3 + 2,ballHeight / 3 + 2,ballHeight / 3 + 2);

//change rotation to match direction (add if statement to be above height of 10 else look straight)
transform.rotation = Quaternion.LookRotation(rigidbody.velocity);


}
  
function hitball() {
	

windSpeed = Random.Range(-15.0, 15.0);
rigidbody.AddForce(Vector3(windx,power,height + windy));
 
	
}

It almost works, but it spins the cube so that the isometric camera can no longer see it. This is a top down golf game, but I have set the gravity on Z so that the golf hole is actually ‘stood’ up in the scene.

I must have tried about 10 different bits of code ‘tweaked’ alas getting nowhere, and I’m sure this is such a SIMPLE thing - that I’m just to new to understand.

Any help appreciated.

Korpers.

You dont need all this Quaternion madness.

You can just directly set an axis of your golf ball to whatever direction you need:

transform.right = rigidbody.velocity;
transform.forward = Vector3.forward;

This would keep the local X axis directed at the velocity and always make sure the local Z axis is directed towards world Z that is the direction to your camera.

Cheers,
Pärtel

It may be a simple thing to fix, but I’m having a heck of a time visualizing how everything is put together, so I’m unclear about what is causing the problem. Let me give you two things to try. If they don’t work, then you’ll need to give me a more detailed description of how everything is put together. Things such as which face of the cube is textured and how the ball moves in the 2D space.

  1. Look rotation takes an optional second parameter for the up vector. By default it is Vector3.up. You can try specifying other axes for this second parameter.

  2. Look rotation assumes you want the positive ‘Z’ direction of the object is the one you want facing the specified vector. If the issue is that the ‘textured’ face of the ball faces the wrong direction if the ball is facing the direction of movement then you can combine rotations. Insert on line 26:

    transform.rotation = transform.rotation * Quaternion.Euler(90,0,0);

Where ‘Quaternion.Euler(90,0,0)’ is a placeholder for some rotation specific to your game.