简体   繁体   English

使 3D 对象看起来像面对 2D 空间中的一个点

[英]Make a 3D Object look like it is facing a point in an 2D space

The first thing you will notice is the complicated and confusing title.您会注意到的第一件事是复杂而令人困惑的标题。 So let me explain that.所以让我解释一下。

I'm trying to make an 2D game in an 3D space using Unity.我正在尝试使用 Unity 在 3D 空间中制作 2D 游戏。 And i'm using a 3D Character as an Player.我使用 3D 角色作为玩家。 This looks like that :这看起来像这样:

图1

As you can see the Background ( A Google map ) is two dimensional.如您所见,背景(Google 地图)是二维的。 While the Player is a 3D Object laying on the ground ( It just Looks like it is Standing ).而玩家是一个躺在地上的 3D 对象(它只是看起来像站立)。

Thats working fine so far.到目前为止工作正常。 But i want that the 3D Character Looks like it is facing a tapped Point on the Background map.但我希望 3D 角色看起来像面对背景地图上的一个点击点。

For example :例如 :

他望着

And two more examples :还有两个例子:

在此处输入图片说明

在此处输入图片说明

The black circle represents the tapped Position.黑色圆圈代表点击的位置。 So i have totally no clue if theres a way to do that, or even if its possible to do that.所以我完全不知道是否有办法做到这一点,或者即使有可能做到这一点。

I tried the following code, but that only rotates my character on an different axis :我尝试了以下代码,但这只会在不同的轴上旋转我的角色:

    Vector3 targetDir = tapped.position - transform.position;

    float step = speed * Time.deltaTime;

    Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);

    transform.rotation = Quaternion.LookRotation(newDir);

Is there even a way to achieve that ?有没有办法实现这一目标? Im currently out of ideas... I would be very glad for any help i can get !我目前没有想法......如果我能得到任何帮助,我会很高兴!

This should do the trick.这应该可以解决问题。 Make your model a child of an unrotated empty, facing is like in your first image, and put the below script on it.让你的模型成为一个未旋转的空子的孩子,就像你的第一张图片一样,然后把下面的脚本放在上面。 I didnt know how you get the point to look at, it is what i tried in unity.我不知道你是如何理解这一点的,这是我在统一中尝试过的。 Hope it helps.希望能帮助到你。

using UnityEngine;

public class LookAtTarget : MonoBehaviour {

    //assign in inspector
    public Collider floor;

    //make sure you Camera is taged as MainCamera, or make it public and assign in inspector
    Camera mainCamera;

    RaycastHit rayHit;

    //not needed when assigned in the inspector    
    void Start() {
        mainCamera = Camera.main;
    }

    void Update () {

        if(Input.GetMouseButtonUp(0)) {

            if(floor.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out rayHit, Mathf.Infinity)) {
                //its actually the inverse of the lookDirection, but thats is because the rotation around z later.
                Vector3 lookDirection = transform.position - rayHit.point;

                float angle = Vector3.Angle(lookDirection, transform.forward);

                transform.rotation = Quaternion.AngleAxis(Vector3.Dot(Vector3.right, lookDirection) > 0 ? angle : angle * -1, Vector3.forward);

            } 

        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM