I’m been constructing my enemy script to follow a target. (as the same as my missils shoud do)
I thought in to use Physics.Raycast() to determine when is facing to the target. otherwise, find the player and determine which side should turn to face again the enemy.
But i’ve been trying to know, how to check if the Y axis is facing to the target ? (Y axis is the front of my objects)
Or how to now that my Z axis angle relative to the target is 0?
if my explanation is hard to understand, i try to explain with other words.
Check out the example, pretty much what you want. If the y axis (green) is really the front of your object though (it shouldn’t be, it should be the z, unless your model imported funky) just change transform.forward to transform.up. But you’re going to have to work around that in all your code, it’d be better just to fix it by parenting the object to an empty gameObject and orienting it correctly, or fixing it at import.
Thank you.
But this is not working… maybe i’m doing somethink wrong in my script.
using UnityEngine;
using System.Collections;
// ENEMY SCRIPT
public class Enemy_Script : MonoBehaviour {
// inspector variables
public float Enemy_Speed = 8f;
public float Enemy_Rotation_Speed = 150f;
public Transform Target;
public Transform Bullet;
public Transform[] Bullet_Sockets;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// turn the enemy to be facing the target
Vector3 targetDir = Target.position - transform.position;
int angle = (int)Vector3.Angle(targetDir, transform.up);
print(angle);
if (angle > 0 angle <= 180) {
transform.Rotate(0, 0, Time.deltaTime * Enemy_Rotation_Speed);
} else if (angle > 180 angle < 360) {
transform.Rotate(0, 0, Time.deltaTime * -Enemy_Rotation_Speed);
}
}
}
If the angle between 1 and 180, turn right. If the angle between 181 and 359, turn left.
If angle is 360 or 0, does not rotate.
An alternative way to approach the problem is to create a vector that gives you the direction from the enemy to the player, then normalize that.
With that vector in hand, you can perform a dot product between the enemy’s forward facing vector and the direction vector you just calculated. Since dot product measures similarity between two vectors, a dot product near or at 1 will indicate the enemy is facing the player at the time, and a negative value will indicate facing away from the player in some capacity.
Where this fails is if the player happens to be exactly to the left or right of the enemy, in which case the dot product will be 0. From there you can figure out where the player is by using the dot product on the computed direction vector and the enemy’s right hand vector to see if the player’s to the right or to the left of the enemy (this will also help determine which direction the enemy should rotate).
void Update ()
{
var dir = (target.transform.position - transform.position).normalized;
var dot = Vector2.Dot(dir, transform.up);
// 1 when they are parallel and same direction , 0 perpendicular to each other,
// -1 parallel opposite direction
Debug.Log(dot);
}
doesn’t work. my gameObject is walking in circle.
i move my “ship” in the scene and nothing happen to him. the value of dot is always changing “-0.3…”, “0.4501…” and keep changing betwiing -1 and 1, but never fix when reach alignment with my ship*.
Tryed using Vector2, Vector3 and Quaternion. but keep the same.
I’m still breaking the head, not to do a bad code, but even so, I’m not getting done.
Even with the approaches described, you’re probably not going to get exactly 1 or -1 due to the way floating point numbers work. You’re going to have to operate with some kind of tolerance. Like, if the dot product is between 0.99 and 1.01. Some relatively small window that the dot product can be in before the ship will do whatever behavior you intend for it.
First, get the local position of your target (which you got)
make sure that the local position y is zero… (just in case.)
Next, find the angle from that local position to Vector3.forward. This tells you the the total angle that you need to turn
Next get the Ceil of the Dot from Vector3.forward to the local position (normalized) This gives you 1 of 3 values… -1, 0 or 1
Next find the maximum amount of rotation you can move
Check the max rotation against the angular difference, if the angular difference is lower, then use it instead. (this prevents jitteryness)
If the Dot is -1 then the amount is negative // of course check this, because its all in theory
rotate the amount.
I’m doing the wrong way.
I was setting the target in design-time not in run-time. that’s why the enemy ship always been walking in circles! him is trying to reach the position of a prefab, not a gameobject in game. So i change my Target variable to private and set it in start() method.