2017-10-11 107 views
1

您会注意到的第一件事是复杂而令人迷惑的标题。 所以让我解释一下。使3D对象看起来像是面对2D空间中的一个点

我正在尝试使用Unity在3D空间中制作2D游戏。我使用3D角色作为播放器。这看起来像这样:

Pic1

正如你所看到的背景(A谷歌地图)是二维的。玩家是躺在地上的3D对象(它看起来像站着)。

这工作很好,迄今。但我希望3D角色看起来像是面对着背景地图上的一个点击点。

例如:

Hes looking towards

和两个例子:

enter image description here

enter image description here

的黑圈代表轻敲的位置。所以我完全不知道是否有办法做到这一点,或者即使它可能做到这一点。

我尝试下面的代码,但只有转动我的性格上的不同轴:

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); 

是甚至有一种方法来实现这一目标?我目前出来的想法... 我会很高兴能得到任何帮助!

+0

只是0的目标方向的y。 – George

+0

@DreamsOfElectricSheep感谢您的快速回答!但那不起作用。玩家总是在地面上“放置”,以创造出他将在其上行走的幻觉。当我将y设置为零时,他突然真的站在了地上O.o – genaray

+0

我认为如果你切换y和z,它应该显示这种效果。如果你的目标方向是(1,0,1),让他面对(1,1,0)。 – yes

回答

1

这应该可以做到。让你的模型成为一个未旋转的空的孩子,面对就像你的第一张图片,并把下面的脚本放在它上面。我不知道你是如何得到这个观点的,这是我在团结中所尝试的。希望能帮助到你。

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); 

      } 

     } 

    } 
} 
+0

感谢您的快速回答!我只是测试它。但不知何故,角色站在地图上,而不是现在平行。他也围绕z轴旋转。他应该与地图平行并围绕x轴旋转,以使他看起来很重要。我只是通过倾听触摸输入和光线投射来获得重点:) – genaray

相关问题