Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by jesser1981 · Dec 01, 2015 at 07:05 PM · c#scripting problemerrortutorial

[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.
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error CS1525: Unexpected symbol 'void' 1 Answer

Menu object not responding 0 Answers

Unexpected symbol error in shader script 0 Answers

"The associated script cannot be loaded." 0 Answers

Help! Values from previous Serialized List<> shows up again after a while using Coroutine 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges