- Home /
Can't equalize float angle
I created a gameobject and reseted transforms. I wanted to calcute the angle between AI and player. And the code in gameobject goes like:
public void calculateAngle(Transform player, Transform AI, float Angle)
{
Debug.DrawRay(AI.position, AI.forward * 8, Color.red);
Debug.DrawLine(AI.position, player.position, Color.black);
Vector3 dir = player.position - AI.position;
Debug.Log(Vector3.Angle(AI.forward, dir));
Angle = Vector3.Angle(AI.forward, dir);
}
And the code in our enemy goes like:
calculateAngle c_A;
public float angle;
Transform player;
void Start()
{
c_A = GameObject.FindGameObjectWithTag("Yonetici").GetComponent<calculateAngle>();
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
c_A.calculateAngle(player, transform, angle);
}
In the console, i can get angle very good, but the angle in the enemy is always 0. For some reason the float Angle in calculateAngle void finds it "unnecessary" to equalize. How can equalize angle in enemy;
Answer by BastianUrbach · Feb 13 at 11:50 AM
By default, parameters in C# are input parameters, meaning you only receive the value passed to the function but modifying it has no effect outside of the method. The easiest fix is to make Angle an output parameter:
public void calculateAngle(Transform player, Transform AI, out float Angle)
c_A.calculateAngle(player, transform, out angle);
However, you might also want to look into how return values work since that seems like a more appropriate solution here.
I solved the problem in different way, i just didn't know that basic stuff, thank you. I learnt more about returns it helped me a lot.