2
votes

I'm trying to figure out a way to move objects on a 3D plane that faces the camera using Unity3D. I want these objects to be constrained to the plane, which itself can move about in 3D space (the camera follows it).

To that end, I thought that I'd use the plane's coordinate system to move the objects. I figured that an x,y coordinate on a plane would have a corresponding x,y,z coordinate in actual 3D space. Problem is, I can't figure out how to access, use, or calculate (x,y) coordinates on that plane.

I've done some research, and I've come across vectors and normals and such, but I don't understand how they pertain to my particular problem. If I'm looking in the right place, could you explain how? And if I'm not, can you point me in the right direction? Thanks a lot.

2

2 Answers

1
votes

I thought that I'd use the plane's coordinate system to move the objects

That's the way to go.

I figured that an x,y coordinate on a plane would have a corresponding x,y,z coordinate in actual 3D space. Problem is, I can't figure out how to access, use, or calculate (x,y) coordinates on that plane.

Yes that's true. But Unity3D gives you access to quite high level functions, so you don't have to do any calculation explicitely.

Set your GameObjects as children of the Plane and move them using local coordinate system.

For example:

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

The code above makes the childObj a child of plane GameObject and move it forward of an offset in its local coordinate system.

0
votes

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