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);
}
}