1

I'm having a hard time to pan a view of a gameObject in Unity3d. I'm new to scripting and I'm trying to develop an AR (Augmented Reality) application for Android.

I need to have a gameObject (e.g. a model of a floor), from the normal top down view, rendered to a "pseudo" iso view, inclined to 45 degrees. As the gameObject is inclined, I need to have a panning function on its view, utilizing four (4) buttons (for left, right, forward(or up), backward(or down)).

The problem is that, I cannot use any of the known panning script snippets around the forum, as the AR camera has to be static in the scene.

Need to mention that, I need the panning function to be active only at the isometric view, (which I already compute with another script), not on top down view. So there must be no problem with the inclination of the axes of the gameObject, right?

Following, are two mockup images of the states, the gameObject (model floor) is rendered and the script code (from Unity reference), that I'm currently using, which is not very much functional for my needs.

enter image description here

enter image description here

Here is the code snippet, for left movement of the gameObject. I use the same with a change in -, +speed values, for the other movements, but I get it only move up, down, not forth, backwards:

#pragma strict

// The target gameObject.
var target: Transform;

// Speed in units per sec.
var speedLeft: float = -10;

private static var isPanLeft = false;


function FixedUpdate()
{
if(isPanLeft == true)
{
    // The step size is equal to speed times frame time.
    var step = speedLeft * Time.deltaTime;

    // Move model position a step closer to the target.
    transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}

static function doPanLeft()
{
    isPanLeft = !isPanLeft;
}

It would be great, if someone be kind enough to take a look at this post, and make a suggestion on how this functionality can be coded the easiest way, as I'm a newbie?

Furthermore, if a sample code or a tutorial can be provided, it will be appreciated, as I can learn from this, a lot. Thank you all in advance for your time and answers.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
CodeBugging
  • 321
  • 12
  • 28

1 Answers1

0

If i understand correctly you have a camera with some fixed rotation and position and you have a object you want to move up/down/left/right from the cameras perspective

To rotated an object to a set of angles you simply do

transform.rotation = Quaternion.Euler(45, 45, 45);

Then to move it you use the cameras up/right/forward in worldspace like this to move it up and left

transform.position += camera.transform.up;
transform.position -= camera.transform.right;

If you only have one camera in your scene you can access its transform by Camera.main.transform

An example of how to move it when someone presses the left arrow

if(Input.GetKeyDown(KeyCode.LeftArrow))
{
    transform.position -= camera.transform.right;
}
Imapler
  • 1,401
  • 12
  • 26
  • Hello @Imapler. Thank you for your time to post your code snippet. Well, I have implemented the following code, according to your example and I get it working ok at Unity play mode, but not when it is build for Android. In Android i got it work only for left-right. Here is the code for all GUI.Buttons: Left button: transform.position += target.transform.right; Right button: transform.position -= target.transform.right; Forward(up) button: transform.position += target.transform.up; Backwards(down) button: transform.position -= target.transform.up; What might be wrong at Android build? – CodeBugging Aug 29 '14 at 11:16
  • What happens when you try to move it up or down on android? – Imapler Aug 29 '14 at 11:56
  • When in android build, up or down (forward, backward) object stays in place.It does not move at all, kind strange as the script seems to be ok. – CodeBugging Aug 29 '14 at 12:51
  • The variable you call target in transform.position += target.transform.up; are you sure its referencing the correct camera. It should be the camera you move the floor model relative to. – Imapler Aug 29 '14 at 13:05
  • The target is the floor model, and the panning scripts are all attached to it. There is only one camera active at scene, and this gameObject (model floor) is moving in cameras' perspective. So, I can't figure out, what is wrong and it does not work in android, but works in play mode. – CodeBugging Aug 29 '14 at 18:02
  • Try change transform.position += target.transform.up; to transform.position += Camera.main.transform.up; – Imapler Aug 29 '14 at 20:41