[HELP] Following the 2D character controller tutorial from the live training section (C#)
Hello,
I have a 2 part questiong/problem, I am following along to the 2D character controllers tutorial from the live training section and I am at the part of the session where it talks about implementing groundcheck. I followed the code precisely but for whatever reason it does not perform a ground check at all. The code does not give me an error or anything at all just does not seem to work.
This is the code I have. Any help would be greatly appreciated. Thank You!
using UnityEngine;
using System.Collections;
public class RobotControllerScript : MonoBehaviour
{
// Variables
public float maxSpeed = 10f; // Controlls the max speed of the character
public float groundRadius = 0.2f; // Creates sphere with a radius of 0.2f
public LayerMask whatIsGround;
public Transform groundCheck;
public float jumpForce = 700f;
private Rigidbody2D myBody;
bool grounded = false;
bool facingRight = true;
Animator anim;
void Awake()
{
myBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate ()
{
// grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround); // Checks to see if the groundCheck is colliding with any other colliders.
// anim.SetBool("Ground", grounded);
// anim.SetBool("vSpeed", myBody.velocity.y);
float move = Input.GetAxis("Horizontal"); // Gets input from the player.
anim.SetFloat("Speed", Mathf.Abs(move));
myBody.velocity = new Vector2(move * maxSpeed,myBody.velocity.y); // Moves the character by the maxSpeed.
if (move > 0 && !facingRight) // Checks to see if the character is facing right and if it's not than flips it to the left
{
Flip();
}
else if (move < 0 && facingRight) // Checks to see if the character is facing the right and if it's not than flips the character to the right.
{
Flip();
}
}
/* void Update()
{
if(grounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("Ground", false);
myBody.AddForce(new Vector2(0, jumpForce));
}
} */
void Flip() // Method to all the character to face in the opposite direction.
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale; // Gets the localScale of the character transform component and assigns it to the variable theScale
theScale.x *= -1; // Takes the value of theScale variable and reverses it on the x axis
transform.localScale = theScale; // Sets the localScale of the character transform and sets it to the value stored in the theScale variable.
}
}
Also because I don't like sitting and doing nothing, I decided to move forward but in the next part of the session, they talk about using the groundcheck to see if the character is grounded and if it is than make the character jump. I received this error
Severity Code Description Project File Line Error CS1503 Argument 2: cannot convert from 'float' to 'bool' Unity Tutorials 2D Infinite Runner Platformer.CSharp C:\Users\jesse\Documents\Unity Game Tutorials\Unity Tutorials 2D Infinite Runner Platformer\Assets_Scripts\Player Scripts\RobotControllerScript.cs 33
This is the line of code that is causing the problem
anim.SetBool("vSpeed", myBody.velocity.y);
Here is the rest of the code again.
using UnityEngine;
using System.Collections;
public class RobotControllerScript : MonoBehaviour
{
// Variables
public float maxSpeed = 10f; // Controlls the max speed of the character
public float groundRadius = 0.2f; // Creates sphere with a radius of 0.2f
public LayerMask whatIsGround;
public Transform groundCheck;
public float jumpForce = 700f;
private Rigidbody2D myBody;
bool grounded = false;
bool facingRight = true;
Animator anim;
void Awake()
{
myBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround); // Checks to see if the groundCheck is colliding with any other colliders.
anim.SetBool("Ground", grounded);
anim.SetBool("vSpeed", myBody.velocity.y);
float move = Input.GetAxis("Horizontal"); // Gets input from the player.
anim.SetFloat("Speed", Mathf.Abs(move));
myBody.velocity = new Vector2(move * maxSpeed,myBody.velocity.y); // Moves the character by the maxSpeed.
if (move > 0 && !facingRight) // Checks to see if the character is facing right and if it's not than flips it to the left
{
Flip();
}
else if (move < 0 && facingRight) // Checks to see if the character is facing the right and if it's not than flips the character to the right.
{
Flip();
}
}
void Update()
{
if(grounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("Ground", false);
myBody.AddForce(new Vector2(0, jumpForce));
}
}
void Flip() // Method to all the character to face in the opposite direction.
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale; // Gets the localScale of the character transform component and assigns it to the variable theScale
theScale.x *= -1; // Takes the value of theScale variable and reverses it on the x axis
transform.localScale = theScale; // Sets the localScale of the character transform and sets it to the value stored in the theScale variable.
}
}