Hello guys , I need some help. I’m trying to understand the codes of this script but for some I have a serious problem . Can someone explain to me well these codes ? Thanks
?
public float moveSpeed;
private float moveVelocity;
public float jumpHeight;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
private Animator anim;
public Transform firePoint;
public GameObject NinjaStar;
public float shotDelay;
private float shotDelayCounter;
public float knockback;
public float knockbackLenght;
public float knockbackCount;
public bool knockFromRight;
private Rigidbody2D myrigidbody2D;
public bool onLadder;
public float climbSpeed;
private float climbVelocity;
private float gravityStore;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
myrigidbody2D = GetComponent<Rigidbody2D>();
gravityStore = myrigidbody2D.gravityScale;
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update () {
if (grounded)
doubleJumped = false;
anim.SetBool("Grounded", grounded);
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
Jump();
}
if (Input.GetKeyDown(KeyCode.Space) && !doubleJumped && !grounded)
{
Jump();
doubleJumped = true;
}
moveVelocity = 0f;
if (Input.GetKey(KeyCode.D))
{
moveVelocity = moveSpeed;
}
if (Input.GetKey(KeyCode.A))
{
//GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
moveVelocity = -moveSpeed;
}
if (knockbackCount <= 0)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
} else
{
if (knockFromRight)
GetComponent<Rigidbody2D>().velocity = new Vector2(-knockback, knockback);
if(!knockFromRight)
GetComponent<Rigidbody2D>().velocity = new Vector2(knockback, knockback);
knockbackCount -= Time.deltaTime;
}
anim.SetFloat("Speed", GetComponent<Rigidbody2D>().velocity.x);
if (GetComponent<Rigidbody2D>().velocity.x > 0)
transform.localScale = new Vector3(1f, 1f, 1f);
else if (GetComponent<Rigidbody2D>().velocity.x < 0)
transform.localScale = new Vector3(-1f, 1f, 1f);
if(Input.GetKeyDown(KeyCode.Return))
{
Instantiate(NinjaStar, firePoint.position, firePoint.rotation);
shotDelayCounter = shotDelay;
}
if(Input.GetKey(KeyCode.Return))
{
shotDelayCounter -= Time.deltaTime;
if(shotDelayCounter <= 0)
{
shotDelayCounter = shotDelay;
Instantiate(NinjaStar, firePoint.position, firePoint.rotation);
}
}
if(onLadder)
{
myrigidbody2D.gravityScale = 0f;
climbVelocity = climbSpeed * Input.GetAxisRaw("Vertical");
myrigidbody2D.velocity = new Vector2(myrigidbody2D.velocity.x, climbSpeed);
}
if(!onLadder)
{
myrigidbody2D.gravityScale = gravityStore;
}
}
public void Jump()
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
}
}
OK, I went through the whole code and did find some strange stuff, but nothing that would not make it “not understandable”
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
// speeds and jump stuff
public float moveSpeed;
private float moveVelocity;
public float jumpHeight;
// ground check appears to be an object
// under the object to test if you are on the ground
public Transform groundCheck;
// radius of that check.
public float groundCheckRadius;
// defines what the ground layer is.
public LayerMask whatIsGround;
// is the object grounded
private bool grounded;
// did we double jump yet?
private bool doubleJumped;
// the animator of the object
private Animator anim;
object for controlling where the point of firing is
public Transform firePoint;
// the thing we are going to fire?
public GameObject NinjaStar;
// the amount of time til the next shot
public float shotDelay;
// how long we should delay after we shoot
private float shotDelayCounter;
// the amount of movement that happens when you are knocked back when hit?
public float knockback;
public float knockbackLenght;
public float knockbackCount;
public bool knockFromRight;
// the rigid body attached to the object
private Rigidbody2D myrigidbody2D;
// ladder stuff
public bool onLadder;
public float climbSpeed;
private float climbVelocity;
private float gravityStore;
// Use this for initialization
void Start () {
// setup the animator
anim = GetComponent<Animator>();
// setup the rigidbody and gravity
myrigidbody2D = GetComponent<Rigidbody2D>();
gravityStore = myrigidbody2D.gravityScale;
}
void FixedUpdate()
{
// every fixed update, see if we are grounded.
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update () {
// reset the doubleJump if we are grounded
if (grounded)
doubleJumped = false;
// set the animator's grounded boolean
anim.SetBool("Grounded", grounded);
// if we are grounded and we press the space key... Jump.
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
Jump();
}
// if we are not grounded, and we have not double jumped
// and we press the space key... jump.
if (Input.GetKeyDown(KeyCode.Space) && !doubleJumped && !grounded)
{
Jump();
doubleJumped = true;
}
// I dont agree with this, but set the move velocity to 0
moveVelocity = 0f;
// capture the A and D keys and set the movement to that key's direction
if (Input.GetKey(KeyCode.D))
{
moveVelocity = moveSpeed;
}
if (Input.GetKey(KeyCode.A))
{
//GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
moveVelocity = -moveSpeed;
}
// knockbackCount is the time until we are not being knocked back.
if (knockbackCount <= 0)
{
// if you are not being knocked back, move with the key
GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
} else
{
// if we are being knocked back, move with the knock.
if (knockFromRight)
GetComponent<Rigidbody2D>().velocity = new Vector2(-knockback, knockback);
if(!knockFromRight)
GetComponent<Rigidbody2D>().velocity = new Vector2(knockback, knockback);
knockbackCount -= Time.deltaTime;
}
// set the animator's speed to correspond to the x movement
anim.SetFloat("Speed", GetComponent<Rigidbody2D>().velocity.x);
// if we are moving one direction, the sprite is left to right
// if we are moving the other, it should be flipped.
if (GetComponent<Rigidbody2D>().velocity.x > 0)
transform.localScale = new Vector3(1f, 1f, 1f);
else if (GetComponent<Rigidbody2D>().velocity.x < 0)
transform.localScale = new Vector3(-1f, 1f, 1f);
// if we press return, throw a star...
if(Input.GetKeyDown(KeyCode.Return))
{
Instantiate(NinjaStar, firePoint.position, firePoint.rotation);
shotDelayCounter = shotDelay;
}
// if we hold return, use shotDelayCounter to time the stars thrown.
if(Input.GetKey(KeyCode.Return))
{
shotDelayCounter -= Time.deltaTime;
if(shotDelayCounter <= 0)
{
shotDelayCounter = shotDelay;
Instantiate(NinjaStar, firePoint.position, firePoint.rotation);
}
}
// if we are on a ladder, we can use this to climb.
// we turn gravity off, this way we can move the sprite up and down.
if(onLadder)
{
myrigidbody2D.gravityScale = 0f;
climbVelocity = climbSpeed * Input.GetAxisRaw("Vertical");
myrigidbody2D.velocity = new Vector2(myrigidbody2D.velocity.x, climbSpeed);
}
if(!onLadder)
{
myrigidbody2D.gravityScale = gravityStore;
}
}
// when we press jump, set the jump height as the upward velocity.
public void Jump()
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
}
}
When you read or write code, try to add comments in so that you or other people can understand it later.
There are assumptions in this code. Like, a NinjaStar will propel its self, or a ladder will set the OnLadder variable.
Thank you so much for answered me! You’ve been a great help ! I just needed to figure out some command because I’m still a rookie of unity . Another help , can you explain to me in detail what do this: “anim.SetBool(“Grounded”, grounded);” ?
anim.SetBool(“Grounded”, grounded);" This line calls in Mecanim Animator bool parameter called “Grounded” and sets it to value of grounded variable. It basically changes state of bool paremeter for that specific Animator.
Thanks a lot for the replies guys , you 're a great help . One last help (I swear ! ) , I need detailed explanation of this last piece of codes … thank you!
public class EnemyPatrol : MonoBehaviour {
public float moveSpeed;
public bool moveRight;
public Transform wallCheck;
public float wallCheckRadius;
public LayerMask whatIsWall;
private bool hittingWall;
private bool notAtEdge;
public Transform edgeCheck;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
hittingWall = Physics2D.OverlapCircle(wallCheck.position, wallCheckRadius, whatIsWall);
notAtEdge = Physics2D.OverlapCircle(edgeCheck.position, wallCheckRadius, whatIsWall);
if (hittingWall || !notAtEdge)
moveRight = !moveRight;
if (moveRight)
{
transform.localScale = new Vector3(-1f, 1f, 1f);
GetComponent<Rigidbody2D>().velocity = new Vector3(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
} else
{
transform.localScale = new Vector3(1f, 1f, 1f);
GetComponent<Rigidbody2D>().velocity = new Vector3(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
}
}
if (shouldChangeDirection because of edge or wall) changeDirection; if (moveRight) { make sure sprite is facing right. set velocity so it moves right. } else { make sure sprite is facing left. set velocity so it moves left. }