Grid Platformer Movement - 5 months trying

Two different issues I can’t seem to tackle on my own.

Self taught online and for what I am trying to do it has been difficult trying to find all the parts/pieces online for the player movement I’m trying to build. Most of the tutorials are in C# and it seems to be harder to find 2D tutorials using Javascript for platformer/grid games.

With that all said, the script below is a Javascript for a grid/unit based player movement.

What the player is doing:

-So far the character moves unit to unit as long as the button is held down, and will float in mid air and drift down slowly (glide) since the character has a rigidbody2D attached to it to make up the gravity for now.

What the player needs to do:

-The player needs to continue moving when either right or left is pressed and jump with gravity if possible. I have the “up” button temporary there until I find a good explanation of how to incorporate gravity to a non-physics moving/forced character. Also down is to stop them for now. I just need very precise controls for timing.

Any help or advice is appreciated. Thank you.

#pragma strict

var startPoint : Vector3;
var endPoint : Vector3;
var speed : float = 0.1; //Speed set to 0.1 helps to smoothly transfer player unit by unit. 
var isMoving : boolean;
var increment : float;
var buttonpressr : boolean; //When right is pressed stay on/off.
var buttonpressl : boolean; //When left is pressed stay on/off.

function Start () {

	startPoint = transform.position; //Start position.
	endPoint = transform.position; //Position after moving.

}

function Update () {

	//Moves player if increment is less than or equal to 1.
	//Moving pace.
	if(increment <=1 && isMoving == true) {
		increment += speed/1;
	
	}
	else {
		isMoving = false;
	}
	
	//When moving by units/increments move smoothly from start point to end point.
	if(isMoving){transform.position = Vector3.Lerp(startPoint, endPoint, increment);}
	
		
	//Input directions and instructions bellow.
	else if(Input.GetKey("left") && isMoving == false) {
	
		increment = 0;
		isMoving = true;
		startPoint = transform.position;
		endPoint = new Vector3(transform.position.x - 1,transform.position.y);
		buttonpressl = true;
		buttonpressr = false;
		
	}
	
	else if(Input.GetKey("right") && isMoving == false) {
	
		increment = 0;
		isMoving = true;
		startPoint = transform.position;
		endPoint = new Vector3(transform.position.x + 1,transform.position.y);
		buttonpressl = false;
		buttonpressr = true;
		
		
	}
			
	else if(Input.GetKey("down") && isMoving == false) {
	
		increment = 0;
		isMoving = false;
		startPoint = transform.position;
		endPoint = new Vector3(transform.position.x,transform.position.y);
		buttonpressl = false;
		buttonpressr = false;
		
	}
		
	else if(Input.GetKey("up") && isMoving == false) {
	
		increment = 0;
		isMoving = true;
		startPoint = transform.position;
		endPoint = new Vector3(transform.position.x,transform.position.y + 2);
		
	}
}

Hey there,

I haven’t actually looked at your code I’m afraid, but here is how I would do it (I have been testing with a Cube with RigidBody and collider attached, if you want to use the 2DRigidbody etc then make sure to change the corresponding parts of the code!)

The set-up of my rigidbody was everything standard except constraints where I have locked the z position and the x, y and z rotations.

private var xIndex : int = 0;
private var lastIndex : int = 0;
var gridSpacing : float = 1.0;
private var actualXPos : float = 0;
private var isMoving : boolean = false;
private var timeStart : float;
var moveTime : float = 0.5;
private var yVelocity : float = 0;
private var grounded : boolean = false;
private var justJumped : boolean = false;
var jumpHeight : float = 5.0;
private var jumpStart : float = 0;

function FixedUpdate(){
	if(!isMoving){
		if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)){
			lastIndex = xIndex;
			xIndex++;
			isMoving = true;
			timeStart = Time.time;
		}else if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)){
			lastIndex = xIndex;
			xIndex--;
			isMoving = true;
			timeStart = Time.time;
		}
	}else{
		actualXPos = Mathf.Lerp(lastIndex*gridSpacing, xIndex*gridSpacing, (Time.time-timeStart)/moveTime);
		if((Time.time-timeStart)/moveTime >= 1){
			isMoving = false;
		}
	}
	
	if(grounded && !justJumped){
		if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)){
			jumpHeight = Mathf.Max(jumpHeight, 0);
			rigidbody.AddForce(Vector3.up*Mathf.Sqrt(-jumpHeight*2*Physics.gravity.y), ForceMode.VelocityChange);
			justJumped = true;
			jumpStart = Time.time;
		}
	}
	if(justJumped && !grounded){
		justJumped = false;
	}
	if(Time.time-jumpStart >= Time.fixedDeltaTime*2){
		justJumped = false;
	}
	
	rigidbody.position.x = actualXPos;
}

function OnCollisionStay(collisionInfo : Collision){
	for(var contact : ContactPoint in collisionInfo.contacts){
		if(contact.normal.y >= 0.9){
			grounded = true;
		}
	}
}

function OnCollisionExit(){
	grounded = false;
}

Hope this helps, let me know if you have any problems!

Scribe