I started to work on a litle projekt an i need help with the scripting. I watched some tutorials but i dont get the code. I know some basic stuf like:
- Variabels
- if/else
- Functions
But everything else is a mistery for me. I would me very happy if someone yould coment this code so i can understand what it dose i dont even get what the point´s are doing or what MyRigidbody2D dose. That the script lets me walk and jump i get but not how it dose it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour {
private Rigidbody2D myRigidbody;
[SerializeField]
private float moveSpeed;
private bool facingRigth;
[SerializeField]
private Transform[ ] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
private bool isGrounded;
private bool jump;
[SerializeField]
private float jumpForce;
private Animator myAnimator;
// Use this for initialization
void Start () {
facingRigth = true;
myRigidbody = GetComponent();
myAnimator = GetComponent();
}
// Update is called once per frame
void Update () {
float horizontal = Input.GetAxis(“Horizontal”);
HandleInput();
isGrounded = IsGrounded();
handleMovement (horizontal);
flip (horizontal);
ResetValues();
}
private void handleMovement (float horizontal)
{
if(isGrounded && jump)
{
isGrounded = false;
myRigidbody.AddForce(new Vector2(0, jumpForce));
}
myRigidbody.velocity = new Vector2 (horizontal * moveSpeed, myRigidbody.velocity.y);
myAnimator.SetFloat(“Speed”, Mathf.Abs(horizontal));
}
private void flip (float horizontal)
{
if (horizontal > 0 && !facingRigth || horizontal < 0 && facingRigth)
{
facingRigth = !facingRigth;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;
}
}
private bool IsGrounded()
{
if (myRigidbody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[ ] collieders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for(int i = 0; i < collieders.Length; i++)
{
if(collieders*.gameObject != gameObject)*
{
return true;
}
}
}
}
return false;
}
private void ResetValues()
{
jump = false;
}
}