I’m going along with a tutorial and all of my code is correct and it looks very similar to the asset of the 2DRobotBoy Asset. In my case for some reason my Player isn’t jumping. Any advice or direction would be appreciated. In The meantime, I’m going to watch more tutorials to see how they applied jumping.
In my code I am actually using is grounded to detect the floor, In my scene I have it set to everything is ground.
Thanks in Advance!
This is my code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private Rigidbody2D myRigidBody;
private Animator myAnimator;
[SerializeField]
private float MovementSpeed;
private bool FacingRight;
[SerializeField]
Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
private bool isGrounded;
private bool jump;
//[SerializeField]
//private bool airControl;
[SerializeField]
private float jumpForce;
//adding a refrences
// Use this for initialization
void Start () {
FacingRight = true;
myRigidBody = GetComponent<Rigidbody2D> ();
myAnimator = GetComponent<Animator> ();
}
// Update is called once per frame
void Update ()
{
}
void FixedUpdate ()
{
float horizontal = Input.GetAxis ("Horizontal"); // looks at the input axis
isGrounded = IsGrounded();
PlayerMovement (horizontal);
Flip (horizontal);
ResetValues ();
}
private void PlayerMovement( float horizontal )
{
myRigidBody.velocity = new Vector2 (horizontal * MovementSpeed, myRigidBody.velocity.y);
myAnimator.SetFloat ("speed", Mathf.Abs (horizontal));
if (isGrounded && jump)
{
isGrounded = false;
myRigidBody.AddForce(new Vector2(0,jumpForce));
}
}
private void PlayerInput()
{
if(Input.GetKeyDown(KeyCode.W))
{
//myRigidBody.AddForce(new Vector2(0f,jumpForce));
jump = true;
}
}
private void Flip(float horizontal )
{
if (horizontal > 0 && !FacingRight || horizontal < 0 && FacingRight)
{
FacingRight = !FacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private void ResetValues()
{
//attack =false
jump = false;
}
private bool IsGrounded()
{
if (myRigidBody.velocity.y <= 0)
{
foreach(Transform point in groundPoints)
{ // colliders
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position,groundRadius,whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders*.gameObject != gameObject)*
-
{*
-
return true;*
-
}*
-
}*
-
}*
-
}*
-
return false;*
-
}*
}