Transform Rotation with Unity3d

Asked

Viewed 9,832 times

2

I’m wanting to develop a game (starting studies now), and I’m having a hard time:

I have a scenario that is basically a planet and on this planet I can put objects that will "aim" at the player... I’ve been seeing some shapes and the most interesting was the function LookAt(Transform), but this method rotates all the axes of the objects that will target the player. I’d like some tips on how I can turn this object into just one axis. Regardless of whether the player jumps, falls or any other action that the user does to go up or down.

Does anyone have any hint of a tutorial or any question regarding it?

2 answers

2

You can use the transform.eulerAngles. this variable is of the type Vector3 and it can be modified, it represents the current rotation angulation X, Y and Z.

Reference: Unity Script Reference Transform.eulerAngles

There is also the function transform.Rotate(). the function will rotate the object according to the angle you enter in Vector3, and the second parameter Space, that would be the rotation relative to space.

Reference: Unity Script Reference Transform. Rotate()

  • 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!

  • 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... 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!

2


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.

  • 1

    Good morning Luiz Vieira, Cara... just like Chichila you helped me a lot... your solution solved almost perfectly my problem. Just a question... in the excerpt you used "Enemy.position" and "Transform.position", I replace "position" with "localPosition". Using only "position", the gameobject had some strange behaviors. Well... that’s it! Thank you very much and hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.