- Home /
Question on Sprite and Movement
Hi, Im making a 2d platform game, im having an issue! Im looking to add sprites but Im not sure how to go about it (Code below) also theres a jumping problem when I press up and right the player moves in that direction, but when the player tries Up and Left they jump right? if anyone could help with either of the problem that would be awesome! I've posted the code below. Thanks! Joe
using UnityEngine;
using System.Collections;
public class PlayerInput : MonoBehaviour {
public float MaxSpeed = 5;
public float Acceleration = 10;
public float JumpSpeed = 6;
public float JumpDuration;
public bool EnableDoubleJump = true;
public bool wallHitDoubleJumpOverride = true;
bool canDoubleJump = true;
float jmpDuration;
bool jumpKeyDown = false;
bool canVariableJump = false;
void Update ()
{
float horizontal = Input.GetAxis ("Horizontal");
if (horizontal < -0.1f)
{
if (rigidbody2D.velocity.x > -this.MaxSpeed)
{
rigidbody2D.AddForce (new Vector2(-this.Acceleration, 0.0f));
}
else
{
rigidbody2D.velocity = new Vector2(-this.MaxSpeed, rigidbody2D.velocity.x);
}
}
else if (horizontal > 0.1f)
{
if (rigidbody2D.velocity.x < this.MaxSpeed)
{
rigidbody2D.AddForce (new Vector2(this.Acceleration, 0.0f));
}
else
{
rigidbody2D.velocity = new Vector2(this.MaxSpeed, rigidbody2D.velocity.y);
}
}
bool onTheGround = isOnGround();
float vertical = Input.GetAxis("Vertical");
if(onTheGround)
{
canDoubleJump = true;
}
if(vertical > 0.1f)
{
if (!jumpKeyDown)
{
jumpKeyDown = true;
if(onTheGround || (canDoubleJump && EnableDoubleJump) || wallHitDoubleJumpOverride)
{
bool wallHit = false;
int wallHitDirection = 0;
bool leftWallHit = isOnWallLeft();
bool rightWallHit = isOnWallRight();
if(horizontal !=0)
{
if(leftWallHit)
{
wallHit = true;
wallHitDirection = 1;
}
else if(rightWallHit)
{
wallHit = true;
wallHitDirection = -1;
}
}
if(!wallHit)
{
if(onTheGround || (canDoubleJump && EnableDoubleJump))
{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, this.JumpSpeed);
JumpDuration = 0.0f;
canVariableJump = true;
}
}
else
{
rigidbody2D.velocity = new Vector2(this.JumpSpeed * wallHitDirection, this.JumpSpeed);
jmpDuration = 0.0f;
canVariableJump = true;
}
if(!onTheGround && !wallHit)
{
canDoubleJump = false;
}
}
}
else if(canVariableJump)
{
jmpDuration += Time.deltaTime;
if(jmpDuration < this.JumpDuration /1000)
{
rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, this.JumpSpeed);
}
}
}
else
{
jumpKeyDown = false;
canVariableJump = false;
}
}
//Methods
private bool isOnGround()
{
float lengthToSearch = 0.1f;
float colliderThreshold = 0.1f;
Vector2 linestart = new Vector2 (this.transform.position.x, this.transform.position.y - this.renderer.bounds.extents.y - colliderThreshold);
Vector2 vectorToSearch = new Vector2 (this.transform.position.x, linestart.y - lengthToSearch);
RaycastHit2D hit = Physics2D.Linecast (linestart, vectorToSearch);
return hit;
}
private bool isOnWallLeft()
{
bool retVal = false;
float lengthToSearch = 0.1f;
float colliderThreshold = 0.1f;
Vector2 linestart = new Vector2 (this.transform.position.x - this.renderer.bounds.extents.x - colliderThreshold, this.transform.position.y);
Vector2 vectorToSearch = new Vector2 (linestart.x - lengthToSearch, this.transform.position.y);
RaycastHit2D hitLeft = Physics2D.Linecast (linestart, vectorToSearch);
retVal = hitLeft;
if (retVal)
{
if(hitLeft.collider.GetComponent<NoSlideJump>())
{
retVal = false;
}
}
return retVal;
}
private bool isOnWallRight()
{
bool retVal = false;
float lengthToSearch = 0.1f;
float colliderThreshold = 0.1f;
Vector2 linestart = new Vector2 (this.transform.position.x + this.renderer.bounds.extents.x + colliderThreshold, this.transform.position.y);
Vector2 vectorToSearch = new Vector2 (linestart.x + lengthToSearch, this.transform.position.y);
RaycastHit2D hitRight = Physics2D.Linecast (linestart, vectorToSearch);
retVal = hitRight;
if (retVal)
{
if(hitRight.collider.GetComponent<NoSlideJump>())
{
retVal = false;
}
}
return retVal;
}
}
Comment