I’m currently working on a game for BPA ((business professionals of America
And this code is for a character that needs to move left, right and jump. Right now the character can indeed move left and right and the animations look good! And I do have the animation for the jump down already. I was following a outdated tutorial for a while but my classmate helped me out with getting it to work, but the one thing we cannot figure out is the jump. I made a ground check, and a bool for ground. All I need now is for the jump to work along with the animations, it would mean a lot if someone could help. Here’s my code!
Extra info would be this is a 2d game and what not.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CInnabunController : MonoBehaviour {
public float maxSpeed = 8f;
bool left = true;
Animator a;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
void Start () {
a = GetComponent<Animator> ();
}
void FixedUpdate () {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
a.SetBool ("Ground", grounded);
a.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity = new Vector2 (0f,3f));
float move = Input.GetAxis ("Horizontal");
a.SetFloat ("Speed", Mathf.Abs (move));
GetComponent<Rigidbody2D> ().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D> ().velocity.y);
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.position += Vector3.left * maxSpeed * Time.deltaTime;
}
else if (Input.GetKey (KeyCode.RightArrow)) {
transform.position += Vector3.right * maxSpeed * Time.deltaTime;
}
if (move > 0 && left)
Flip ();
else {
if (move < 0 && !left)
Flip ();
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow) && GetComponent<Rigidbody2D>().velocity = new Vector2(0f,3f)){
a.SetBool ("Ground", false);
}
}
void Flip()
{
left = !left;
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
}
}
// GetComponant().velocity = new Vector3 (x,y,z) float = f,