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 /
avatar image
0
Question by Kimone92 · May 07, 2016 at 04:15 PM · scripting problemerrorscripting beginnerscript errorvoid

Help with script

alt text


private Animator myAnimator;

 [SerializeField]
 private float movementSpeed;

 private bool facingRight;

 [SerializeField]
 private Transform[] groundPoints;

 [SerializeField]
 private float groundRadius;

 [SerializeField]
 private LayerMask whatIsGround;

 [SerializeField]
 private bool airControl;

 [SerializeField]
 private float jumpForce;

 public Rigidbody2D MyRigibody { get; set; }

 public bool Attack { get; set; }

 public bool Jump { get; set; }

 public bool OnGround { get; set; }

 // Use this for initialization
 void Start ()
 {
     facingRight = true;
     MyRigibody = GetComponent<Rigidbody2D> ();
     myAnimator = GetComponent<Animator> ();
 }

 void Update()
 {
     HandleInput();
 }

 // Update is called once per frame
 void FixedUpdate()
 {
     float horizontal = Input.GetAxis ("Horizontal");


     OnGround = IsGrounded();


     HandleMovement(horizontal);


     Flip(horizontal);

     HandleLayers ();
 }

 private void HandleMovement (float horizontal)
 {
     
     if (MyRigibody.velocity.y < 0)
     {
         myAnimator.SetBool ("land", true);
     }
     if (!Attack && (OnGround || airControl))
     {
         MyRigibody.velocity = new Vector2 (horizontal * movementSpeed, myAnimator.velocity.y);
     }
     if (Jump && MyRigibody.velocity.y == 0)
     {
         MyRigibody.AddForce(new Vector2(0, jumpForce));
     }

     myAnimator.SetFloat ("speed", Mathf.Abs (horizontal));
 }

     private void HandleInput()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
         
         }
         if (Input.GetKeyDown(KeyCode.LeftShift))
         {
         
         }
     }

     private void Flip (float horizontal)
     {
         if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
         {
             facingRight = !facingRight;

             Vector3 theScale = transform.localScale;

             theScale.x *= -1;

             transform.localScale = theScale;
         }
     }

 private bool IsGrounded()
 {
     if (MyRigibody.velocity.y <= 0)
         {
             foreach (Transform point in groundPoints)
             {
                 Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);

                 for (int i = 0; i < colliders.Length; i++)
                 {
                 if (colliders[i].gameObject != gameObject)
                     return true;
                 }

                 }

             }

         }
         return false;
     }

 private void HandleLayers()
 {
     if (!OnGround)
 {
         myAnimator.SetLayerWeight (1, 1);    
 }
 else
 {
         myAnimator.SetLayerWeight (1, 0);
 }
 }

}

screen-shot-2016-05-07-at-150451.png (206.8 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TBruce · May 07, 2016 at 04:24 PM

@Kimone92

return false; was outside the function and you had an extra curly brace

 public class Player : MonoBehaviour
 {
     private Animator myAnimator;
     [SerializeField]
     private float movementSpeed;
     private bool facingRight;
     [SerializeField]
     private Transform[] groundPoints;
     [SerializeField]
     private float groundRadius;
     [SerializeField]
     private LayerMask whatIsGround;
     [SerializeField]
     private bool airControl;
     [SerializeField]
     private float jumpForce;
     public Rigidbody2D MyRigibody { get; set; }
     public bool Attack { get; set; }
     public bool Jump { get; set; }
     public bool OnGround { get; set; }
 
     // Use this for initialization
     void Start ()
     {
         facingRight = true;
         MyRigibody = GetComponent<Rigidbody2D> ();
         myAnimator = GetComponent<Animator> ();
     }
 
     void Update()
     {
         HandleInput();
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         float horizontal = Input.GetAxis ("Horizontal");
         OnGround = IsGrounded();
         HandleMovement(horizontal);
         Flip(horizontal);
         HandleLayers ();
     }
 
     private void HandleMovement (float horizontal)
     {
         if (MyRigibody.velocity.y < 0)
         {
             myAnimator.SetBool ("land", true);
         }
         if (!Attack && (OnGround || airControl))
         {
             MyRigibody.velocity = new Vector2 (horizontal * movementSpeed, myAnimator.velocity.y);
         }
         if (Jump && MyRigibody.velocity.y == 0)
         {
             MyRigibody.AddForce(new Vector2(0, jumpForce));
         }
         myAnimator.SetFloat ("speed", Mathf.Abs (horizontal));
     }
 
     private void HandleInput()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
 
         }
         if (Input.GetKeyDown(KeyCode.LeftShift))
         {
 
         }
     }
 
     private void Flip (float horizontal)
     {
         if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
         {
             facingRight = !facingRight;
             Vector3 theScale = transform.localScale;
             theScale.x *= -1;
             transform.localScale = theScale;
         }
     }
 
     private bool IsGrounded()
     {
         if (MyRigibody.velocity.y <= 0)
         {
             foreach (Transform point in groundPoints)
             {
                 Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
                 for (int i = 0; i < colliders.Length; i++)
                 {
                     if (colliders[i].gameObject != gameObject)
                     return true;
                 }
             }
         }
         return false;
     }
 
     private void HandleLayers()
     {
         if (!OnGround)
         {
             myAnimator.SetLayerWeight (1, 1);    
         }
         else
         {
             myAnimator.SetLayerWeight (1, 0);
         }
     }
 }
Comment
Add comment · Show 6 · Share
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
avatar image Kimone92 · May 07, 2016 at 04:57 PM 0
Share

Why thank you very much! ^^ Saved my day

avatar image MD_Reptile Kimone92 · May 07, 2016 at 04:58 PM 0
Share

I've converted your answer to a comment. In the future please use the comment button if you aren't sharing a possible solution.

avatar image TBruce MD_Reptile · May 07, 2016 at 05:12 PM 0
Share

What do you mean not sharing solution? $$anonymous$$y answer was shared with everyone.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't add script to anything error 1 Answer

How do I make do I make a variable go back to its original value for a spell system 1 Answer

Rocket Projectiles in Flight Simulator error 1 Answer

weird problem with scripting 1 Answer

How to know exactly which game objects and objects are not referenced ? 1 Answer


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