Very weird. Transform position is at two places at the same time.
So, i'm trying to get a direction, but somehow it's in the completely wrong position, seeming to be endlessly far away.
My gameobjects also show up in the sky, but i think that is just my other cameras seeing them, because i couldn't find any gameobjects there, yet the target direction seems to point that way.
I hope you can shed some light on my situation. By looking at this piece of code and a picture it might make more sense.
Here are the variables
GameObject current_enemy;
self = transform.position + new Vector3(0f, 1f, 0f);
float angleToEnemy;
Vector3 targetDirection;
This is where i get the direction to the target.
void CheckIfFacingEnemy() {
targetDirection = self + current_enemy.transform.position;
angleToEnemy = Vector3.Angle(targetDirection, transform.forward);
if(angleToEnemy < 40) { _facingEnemy = true; }
else { _facingEnemy = false; }
}
and heres gizmos
void OnDrawGizmosSelected() {
Gizmos.color = Color.red;
Gizmos.DrawRay(transform.position + new Vector3(0, 1, 0), targetDirection);
Gizmos.DrawSphere(current_enemy.transform.position, 2);
}
and this picture show that the current enemy is at the right place on the ground, but also showing up in the sky, where it seems to point...
Keep in mind that i can't find any gameobjects in the sky where you can see the reflections.
The only thing i know is that my confusion level is way over 9000.
If i didn't provide enough information, please let me know.
Answer by Soos621 · Jul 27, 2016 at 01:31 PM
Your target direction should just be:
Vector3 targetDirection = otherObject.position - thisObject.position;
You don't need your origin because you are raycasting from the point of origin towards the direction but always subtract the vector3 origin point from the target vector3
As a note you can also find out if one object is facing the other with vector3.dot example
Vector3 direction = target.transform.position - this.transform.position;
Float dProduct = Vector3.dot(this.tranform.forward, direction);
If(dProduct > .5){ // Is facing object }
Trying it this way made me realize that the problem was my Self variable. I provided bad code, because the Self variable was from other script, and i had a Self variable in this script as well, but i had not assigned it so it was vector 3.zero. This help room is brilliant! Thank you!