How to get an angle with sine or cosine?

Hi everybody,
One of my scripts calculates the cosine of an angle by dividing one distance by another. My question is:
How can I get the angle when I have the cosine or sine? :?:

Use Mathf.Asin and Mathf.Acos (Mathf.Acos(x) basically means “the angle whose cosine is x”).

Alternatively, you might be able to represent your distances as vector offsets from a corner point and use Vector3.Angle to get the result you want. No trigonometry required.

1 Like

Thanks andeeee :slight_smile:

But it’s not working…
In my game, I want a character with a weapon to shoot on a point (let’s call it point A) in the center of the field of view. I created the character with 5 different shoot animations for 5 different angles.
The problem is that I can’t get the angle beetween the point A and the characters z-axis. I tried to use trigonometry, to access the main cameras rotation (by camera.transform.rotation.x) and many other ways but nothing happens, the character always schoots in one direction. :?

This is the targeting part of my code:

function Update () {
	var target : RaycastHit;
	if (Physics.Linecast (MainCamera.position, endPoint.position, target)) {
	}

	cameraAngle = Vector3.Angle(transform.forward,target.point);

	// Play the shoot animation
	if (Input.GetButtonDown ("Fire1")) {
		// We are in idle so play it on the fully body
		if (cameraAngle <= shootHeight1) {
			animation.CrossFadeQueued("shoot2", 0.0, QueueMode.PlayNow);
		}
		if (targetAngle >= shootHeight1) {
			animation.CrossFadeQueued("shoot-2", 0.0, QueueMode.PlayNow);
		}
	}
}

This is my character:

108842--4166--$bild_1_171.png

What you probably want is something like:-

var targetHeading = target.point - transform.position;
cameraAngle = Vector3.Angle(transform.forward, targetHeading);

The targetHeading variable contains the position of the target relative to the player.

Thank you again Andeeee!
The character now indicates the correct angle… :slight_smile:
The last problem I have is that the character only shoots upwards, but no longer in the other directions :?
Maybe there’s an error in the animation part of the script? :?:

The solution for all my problems was sooo simple:

cameraAngle = mainCamera.eulerAngles.x