【问题标题】:points on a plane in 3D3D平面上的点
【发布时间】:2013-05-18 16:18:27
【问题描述】:

我正在尝试找出一种方法,使用Unity3D 在面向相机的 3D 平面上移动对象。我希望这些对象被限制在平面上,它本身可以在 3D 空间中移动(相机跟随它)。

为此,我认为我会使用平面的坐标系来移动对象。我认为平面上的 x,y 坐标将在实际 3D 空间中具有相应的 x,y,z 坐标。问题是,我不知道如何访问、使用或计算该平面上的 (x,y) 坐标。

我做了一些研究,遇到了向量和法线等,但我不明白它们与我的特定问题有何关系。如果我在正确的地方寻找,你能解释一下吗?如果我不是,你能指出我正确的方向吗?非常感谢。

【问题讨论】:

标签: 3d unity3d coordinate-systems plane


【解决方案1】:

我想我会使用飞机的坐标系来移动 对象

这就是要走的路。

我认为平面上的 x,y 坐标会有对应的 实际 3D 空间中的 x,y,z 坐标。问题是,我想不通 如何访问、使用或计算该平面上的 (x,y) 坐标。

是的,这是真的。但是Unity3D 让您可以访问相当高级的函数,因此您不必明确地进行任何计算。

将您的GameObjects 设置为Plane 的子级,并使用本地坐标系移动它们。

例如:

childObj.transform.parent = plane.transform;
childObj.transform.localPosition += childObj.transform.forward * offset;

上面的代码使childObj 成为平面GameObject 的子对象,并将其向前移动到其局部坐标系中的偏移量。

【讨论】:

  • 非常感谢。我会试一试(刚下班回家。有点累!)。
  • @HeisenbugOk,我已经给了这个公平的机会,但它不适合我。而且我也刚刚意识到按 Enter 会保存评论,并且不会输入新行。
  • 我有这样的东西: var playfield : Playfield;函数开始(){ transform.parent = playfield.transform; transform.localRotation = Quaternion.identity; transform.localPosition += moveVector; 此脚本附加到我的“玩家”对象,除非该对象是编辑器中的游戏场对象(我的飞机)的父对象,否则将无法正常工作。因此,此方法不适用于在运行时实例化的任何内容(即……其他所有内容)。我究竟做错了什么? (除了发布代码块......对不起,我无法弄清楚!)
  • @user2048881:您应该能够通过脚本为对象设置父级。编辑您的问题并发布整个代码或更好的 SSCCE。我会尽可能地看看它。
【解决方案2】:

@Heisenbug:

#pragma strict

var myTransform : Transform; // for caching
var playfield : GameObject;
playfield = new GameObject("Playfield");
var gameManager : GameManager; // used to call all global game related functions
var playerSpeed : float;
var horizontalMovementLimit : float; // stops the player leaving the view
var verticalMovementLimit : float;
var fireRate : float = 0.1; // time between shots
private var nextFire : float = 0; // used to time the next shot
var playerBulletSpeed : float;



function Start () 
{
    //playfield = Playfield;
    myTransform = transform; // caching the transform is faster than accessing 'transform' directly
    myTransform.parent = playfield.transform;
    print(myTransform.parent);
    myTransform.localRotation = Quaternion.identity;

}

function Update () 
{
    // read movement inputs
    var horizontalMove = (playerSpeed * Input.GetAxis("Horizontal")) * Time.deltaTime;
    var verticalMove = (playerSpeed * Input.GetAxis("Vertical")) * Time.deltaTime;
    var moveVector = new Vector3(horizontalMove, 0, verticalMove);
    moveVector = Vector3.ClampMagnitude(moveVector, playerSpeed * Time.deltaTime); // prevents the player moving above its max speed on diagonals

    // move the player
    myTransform.localPosition += moveVector;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    相关资源
    最近更新 更多