Assuming you want to fix the y-axis (that is, make your object rotate along the x-z plane only), you just run the Transform::LookAt
passing as parameter a new vector in which the coordinates x and z are of the target object and the coordinate y is of the object itself being rotated.
In this example (in C#), suppose that this script is attached to the object being rotated (the cannon) and that there is another object (the enemy that the cannon must hit) that was linked via editor to the property "Enemy":
using UnityEngine;
using System.Collections;
public class RotateTowards : MonoBehaviour {
public Transform enemy;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 vTarget = new Vector3 (enemy.position.x, transform.position.y, enemy.position.z);
transform.LookAt(vTarget);
}
}
Thus, at each frame of the game, a new vector almost equal to the enemy’s position vector (differing only on the y-axis, where the cannon coordinate was maintained) is passed to the method LookAt
(which will rotate the cannon to "point" to the position in the given space).
Important: Note that in Unity the convention is that the z axis points forward to the object, so functions like the LookAt
(freely translating: "look at") are based on this vector for the calculations. If your object is not rotating as you wish, you should modify it so that the "front" part is pointing to the z axis (There may be some way to automatically adjust this in Unity, but I honestly don’t know - as far as I know this is defined externally in the 3D tool used in character modeling or game object).
This example I suggested requires that the enemy be linked to the cannon, but in a real game this is not usually done this way because there are different enemies. In this case, the suggestion to use a Collier is cool, as you can receive collision events and rotate the cannon to the object (enemy) that entered the "threat area". The best researcher for your case seems to be a sphere centered on the cannon, whose radius is configured to define its range area.
There is also the possibility to create a global list of enemies and check for each one if they are below in a pre-defined circular distance, but this turns out to be a proper implementation of a crash test.
Thank you for answering Chinchilla! Well... I had already analyzed these options and had problems with them for the following reason: These options, solved when I had a certain independent rotation of "target" (that’s why I cited the Lookat(Transform) function, which focuses on a target). What I need is something like a "reach area", when the player is in that area, my object should focus on the player (like a tower of Tower Defense), otherwise the object should assume its original position. I’ve been reading something about Quaternions, but I couldn’t get it right. = ( Again thanks and hugs!
– Carlos Domiciano Junior
Use a Collider to create hitbox, and you add to Trigger when the object touches the collision field activates the function
LookAt
. The programming of Trigger is more or less like this: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html– Chichila
Chichila... I did this, and to tell the truth this code is even commented because Lookat(Transform) targets the Z axis of the Gameobject for the "target". This inevitably rotates the X and Y axes... I need only one of the axes to move (more or less like the clock hand, which only rotates on an axis), causing my object to look at the target, but only changing the Y axis (taking into account the original positioning of the object in the scene). Hugs and again, thank you very much for the reply!
– Carlos Domiciano Junior