Restraining player movement (2d ios)

Hello All!

I’m extremely new to unity (big shocker right?) and I’m working on an iOS game trying to understand a few things. I’ve modified my 2d side scroller script to where the camera is locked into place and my ‘player’ is hovering above the ground and constrained to a certain y coordinate (it’s going to be a helicopter).

What I can’t seem to figure out how to do is prevent my player from moving off screen. From what I’ve researched (i.e. looked up on google) I should be using Mathf.Clamp but I don’t quite understand where it would be used in the script for player movement. Below is my script, if anyone has any recommendations I’d greatly appreciate it!

//////////////////////////////////////////////////////////////
// SidescrollControl.js
//
// SidescrollControl creates a 2D control scheme where the left
// pad is used to move the character, and the right pad is used
// to make the character jump.
//////////////////////////////////////////////////////////////

#pragma strict

@script RequireComponent( CharacterController )

// This script must be attached to a GameObject that has a CharacterController
var moveTouchPad : Joystick;
var jumpTouchPad : Joystick;

var forwardSpeed : float = 4;
var backwardSpeed : float = 4;
var jumpSpeed : float = 16;
var inAirMultiplier : float = 0.25;					// Limiter for ground speed while jumping

private var thisTransform : Transform;
private var character : CharacterController;
private var velocity : Vector3;						// Used for continuing momentum while in air
private var canJump = true;

function Start()
{
	// Cache component lookup at startup instead of doing this every frame		
	thisTransform = GetComponent( Transform );
	character = GetComponent( CharacterController );	

	// Move the character to the correct start position in the level, if one exists
	var spawn = GameObject.Find( "PlayerSpawn" );
	if ( spawn )
		thisTransform.position = spawn.transform.position;
}

function OnEndGame()
{
	// Disable joystick when the game ends	
	moveTouchPad.Disable();	
	jumpTouchPad.Disable();	

	// Don't allow any more control changes when the game ends
	this.enabled = false;
}

function Update()
{
	var movement = Vector3.zero;

	// Apply movement from move joystick
	if ( moveTouchPad.position.x > 0 )
		movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
	else
		movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;
			
	movement += velocity;	
	//movement += Physics.gravity;
	movement *= Time.deltaTime;
	
	// Actually move the character
	character.Move( movement );
	
	if ( character.isGrounded )
		// Remove any persistent velocity after landing	
		velocity = Vector3.zero;
}

I see a few options, depending on your specific needs:

  • Just put a collider in the way. Very simple.
  • Clamp the character’s position after calling Move(). Still pretty simple.
  • Clamp your argument to Move() so that the character isn’t moved off-screen. Not simple.

If an invisible collider will meet your needs, you should really consider it.

If not, then you’ll need to express in clear math the exact space your character is allowed to occupy. If we assume for the sake of argument that all gameplay should be taking place between 0 and 10 on the y axis, you could make a call like this:

var pos = transform.position;
pos.y = Mathf.Clamp(pos.y, 0f, 10f);
transform.position = pos;

Either of those options can probably get you started, yes?

I think it would be easiest to just put up an invisible collider, if I just put a cube up; how would I make it invisible?

If I did use the Mathf.Clamp; would that be a separate script? I’m still very new to unity and how scripting objects works. Part of my problem is trying to understand exactly what’s happening in the 2d side-scroller script that I’m using as the base for my movement.

Thanks for the help : )

To make the the collider invisible, just go into the inspector and uncheck the “Mesh Renderer”. That way it does not show, or tax any polygons on the GPU.

Hope this helps!