minigame inside game

Im trying to have a little minigame inside the main game, but I´m having some problems… I dont know how to explain it, so I uploaded a little video where you can see that it isnt working.

http://www.youtube.com/watch?v=3tJgd3MZf1U

Im puting the objects of the minigame as child objects of the camera. Here is the code.And tell me if there is a better way to do this. Thank you

var startPosition : Vector3;
var endPosition : Vector3;
var mySpeed : float = 0.3;

function Update()
{
startPosition = GameObject.FindWithTag("StartPosition").transform.position;
endPosition = GameObject.FindWithTag("EndPosition").transform.position;
}

function FixedUpdate () 
{
  transform.Translate(Vector3.right*mySpeed*Time.deltaTime);
  if((transform.position - startPosition).sqrMagnitude > (endPosition - startPosition).sqrMagnitude)
  {
	transform.position = startPosition;
  }
}

the problem is that when you jump… the start position is set wrong… but I dont know what should I do…

Well here is a video of the minigame working, fine. What I whant is to be able to play that in the other game, like the first video. :S I dont know if im explaining it ok. Thank you for your help! .

http://www.youtube.com/watch?v=I00L3Sef2HQ

transform.position returns the position in world space and therefore when you jump it will be higher than when you’re on ground. You set the worldposition when you reach the end position. That’s why it’s offset locally when you jump at this point. The movement is only relativly done with Translate so the height will stay. You should use localPosition instead of position.

And:

  • don’t use GameObject.FindWithTag each frame, do it once at start and save the reference to the Transform.
  • If you have the two references saved you don’t need to copy the position into seperate variables.
  • You don’t use Physics or any acceleration so stay out of FixedUpdate, do everything in Update.

Here’s the script a bit optimised :wink:

private var startPosition : Transform;
private var endPosition : Transform;
var mySpeed : float = 0.3;

function Start()
{
    startPosition = GameObject.FindWithTag("StartPosition").transform;
    endPosition = GameObject.FindWithTag("EndPosition").transform;
}

function Update()
{
    transform.Translate(Vector3.right*mySpeed*Time.deltaTime);
    if((transform.localPosition - startPosition.localPosition).sqrMagnitude > (endPosition.localPosition - startPosition.localPosition).sqrMagnitude)
    {
        transform.localPosition = startPosition.localPosition;
    }
}