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 /
This question was closed Oct 20, 2015 at 11:09 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by The_Unity_Game_Developer · Oct 19, 2015 at 04:47 PM · 2dmovementplatformer

C# script broke, I can't fix it

Hello all!

I'm making a 2D platformer game, and everything has been going well, until I added my jump sound effect.

For some reason, it affected the script so that when you try to move you can't. And after attempting to press the left or right arrows (quickly) you can only move in that direction and can't change it.

I don't get what's wrong with it. I tried starting the script again, but the exact same thing happened again. I have no idea about what happened, because I only added one line of code to the jump function, which has nothing to do with the movement. Here is the code:

PlayerController.cs:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float moveSpeed;
     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 projectile;
 
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator>();
     }
 
     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)
         {
             //rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHeight);
             Jump();
         }
 
         if (Input.GetKeyDown(KeyCode.Space) && !doubleJumped && !grounded)
         {
             //rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHeight);
             Jump();
             doubleJumped = true;
         }
 
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             rigidbody2D.velocity = new Vector2(-moveSpeed, rigidbody2D.velocity.y);
 
             if (Input.GetKey(KeyCode.RightArrow))
             {
                 rigidbody2D.velocity = new Vector2(moveSpeed, rigidbody2D.velocity.y);
 
                 anim.SetFloat("Speed", Mathf.Abs(rigidbody2D.velocity.x));
 
                 if (rigidbody2D.velocity.x > 0)
                     transform.localScale = new Vector3(0.3294535f, 0.3294535f, 0.3294535f);
                 else if (rigidbody2D.velocity.x < 0)
                     transform.localScale = new Vector3(-0.3294535f, 0.3294535f, 0.3294535f);
 
                 if (Input.GetKeyDown(KeyCode.Return))
                 {
                     Instantiate(projectile, firePoint.position, firePoint.rotation);
                 }
             }
         }
     }
 
     void Jump() {
         rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHeight);
         audio.Play();
     }
 }

I have the AudioSource on my Player object, and this code returns no errors. When it was working fine, the movement was done so that you push the appropriate key and in moves in that direction, then stops when you release said key. I explained what is wrong above the code.

Thanks in advance!

Comment
Add comment · Show 4
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 The_Unity_Game_Developer · Oct 19, 2015 at 04:49 PM 0
Share

The title is meant to say 'can't' and not that jiibberish

avatar image NerdClown · Oct 19, 2015 at 08:52 PM 0
Share

I think something else happened beyond the audio source.

      if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
      {
          rigidbody2D.velocity = new Vector2(-moveSpeed, rigidbody2D.velocity.y);
 
          if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
          {

This code will only check for right arrow if Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow) returns true. $$anonymous$$aybe two of your } somehow ended up further down in the script than they should be?

avatar image The_Unity_Game_Developer · Oct 20, 2015 at 06:38 AM 0
Share

The Answer from earlier told me that, which worked for some of it, then I just realised that I had friction down to 0 so it was just sliding around. I fixed that.

avatar image NerdClown · Oct 20, 2015 at 07:11 AM 0
Share

Aye, I was overtaken by $$anonymous$$jell while my post was in moderation ... new user so my posts need to be looked over by a moderator before they show up, in case I get all offensive and such. We all know how offensive programmers can be!

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Kjell-Andersson · Oct 19, 2015 at 06:16 PM

Your curl brackets are wrong. You don't want the if (Input.GetKey(KeyCode.RightArrow)) inside the if (Input.GetKey(KeyCode.LeftArrow)) since then you would have to press both the Right and the Left button at once.

To get to the if (Input.GetKeyDown(KeyCode.Return)) statement you have to hold down all three.

Try this instead:

 if (Input.GetKey(KeyCode.LeftArrow))
 {
     rigidbody2D.velocity = new Vector2(-moveSpeed, rigidbody2D.velocity.y);
 }
 else if (Input.GetKey(KeyCode.RightArrow))
 {
     rigidbody2D.velocity = new Vector2(moveSpeed, rigidbody2D.velocity.y);
 }
 else
 {
     rigidbody2D.velocity = new Vector2(0, rigidbody2D.velocity.y);
 }
 
 anim.SetFloat("Speed", Mathf.Abs(rigidbody2D.velocity.x));
 
 if (rigidbody2D.velocity.x > 0)
     transform.localScale = new Vector3(0.3294535f, 0.3294535f, 0.3294535f);
 else if (rigidbody2D.velocity.x < 0)
     transform.localScale = new Vector3(-0.3294535f, 0.3294535f, 0.3294535f);
 
 if (Input.GetKeyDown(KeyCode.Return))
 {
     Instantiate(projectile, firePoint.position, firePoint.rotation);
 }
 
Comment
Add comment · Show 2 · 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 The_Unity_Game_Developer · Oct 19, 2015 at 06:44 PM 0
Share

I've used that code and it works like it did now, but there is still one problem. When you start moving, you don't stop until something stops you. Any ideas?

avatar image Jaws959 · Oct 20, 2015 at 10:57 AM 0
Share

If your just looking to move the object in a simple character movement script, try using transform.position += new vector2 () ins$$anonymous$$d of adding velocity and force.

Alternatively try using the += and -= to use its current velocity in the parameter for your vector 2's

If im right this should slow you down and speed you up. You will have to put a limiter on your velocity still

Follow this Question

Answers Answers and Comments

41 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 avatar image avatar image

Related Questions

Can't move while crouched 0 Answers

how long a bottom being pressed [solved] 3 Answers

How To Make My Player Only Move on the ground? 1 Answer

Unity2D Player Jump Heights Inconsistent,Unity2D Inconsistent Player Jump Heights 0 Answers

Android 2D platformer, low fps 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