Help us understand the problem. What is going on with this article?

2D用のLookAt関数を作った

More than 5 years have passed since last update.

概要

Unityの transform.LookAt メソッドは、任意のオブジェクトを指定するとその方向を向いてくれる便利なメソッドです。相手の方向に飛ぶ弾とかその他諸々活用する機会は多いです。

http://docs.unity3d.com/ScriptReference/Transform.LookAt.html

しかし、2Dゲームでこのメソッドを使うとちょっと不便です。

2DでLookAtを使った場合


例えば左側のキャラを右のCubeの方向に向かせたいとき(画像左)にLookAtを使うとペラッペラな感じになります(画像右)

LookAtメソッドはオブジェクトのZ軸を相手の方向に向かせるので、これは仕方ないです。
なので自分で作った方が良さそうです。
下記の感じでTransformを拡張するクラスを作りました

using UnityEngine;
using System.Collections;

public static class TransformExtensions {
    /// <summary>
    /// 指定のオブジェクトの方向に回転する
    /// </summary>
    /// <param name="self">Self.</param>
    /// <param name="target">Target.</param>
    /// <param name="forward">正面方向</param>
    public static void LookAt2D(this Transform self, Transform target, Vector2 forward)
    {
        LookAt2D (self, target.position, forward);
    }

    public static void LookAt2D(this Transform self, Vector3 target, Vector2 forward)
    {
        var forwardDiff = GetForwardDiffPoint (forward);
        Vector3 direction = target - self.position;
        float angle = Mathf.Atan2(direction.y,direction.x) * Mathf.Rad2Deg;
        self.rotation = Quaternion.AngleAxis(angle - forwardDiff, Vector3.forward);
    }

    /// <summary>
    /// 正面の方向の差分を算出する
    /// </summary>
    /// <returns>The forward diff point.</returns>
    /// <param name="forward">Forward.</param>
    static private float GetForwardDiffPoint(Vector2 forward)
    {
        if (Equals (forward, Vector2.up)) return 90;
        if (Equals (forward, Vector2.right)) return 0;
        return 0;
    }
}

使い方

if (Input.GetKeyDown (KeyCode.Y)) {
    transform.LookAt2D(enemy.transform,Vector2.up);
}
if (Input.GetKeyDown (KeyCode.X)) {
    transform.LookAt2D(enemy.transform,Vector2.right);
}

上の例で実行すると、Yを押すとオブジェクトのY軸が相手の方向を向いてくれて、
Xを押すとオブジェクトのX軸が向いてくれます。

3Dだと大体正面はZ軸だけど、2Dは上向きとか右向きとかあるのでお好みで。

でもUnity5辺りで普通に実装されそうだなあ・・。

adarapata
え、この壷を買うだけでみんなストックしてくれるんですか?
http://adarapata.hatenablog.com/
mixi
全ての人に心地よいつながりを
http://mixi.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした