Search Unity

PlayerMovement Script I made isn't working!

Discussion in 'Getting Started' started by SpongeBob135, Dec 26, 2017.

  1. SpongeBob135

    SpongeBob135

    Joined:
    Dec 26, 2017
    Posts:
    8
    Hi, I've been working on a script to enable my player to move, but it won't work. I get the same error messages each time:
    - The best overloaded method match for `UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2, UnityEngine.ForceMode2D)' has some invalid arguments.
    - Assets/Player_Move.cs(21,16): error CS1503: Argument `#1' cannot convert `float' expression to type `UnityEngine.Vector2'


    Here is my code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Player_Move : MonoBehaviour {

    public Rigidbody2D rb;

    public float sidewaysForce = 500f;

    void FixedUpdate ()
    {

    if (Input.GetKey("d"))
    {
    rb.AddForce(sidewaysForce * Time.deltaTime, 0);
    }

    if (Input.GetKey("a"))
    {
    rb.AddForce(-sidewaysForce * Time.deltaTime, 0);
    }
    }
    }
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Welcome!

    First, please use code tags when sharing code on the forums. It makes it much easier for us to read.

    I give everyone one free formatting, so here's yours:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player_Move : MonoBehaviour {
    6.     public Rigidbody2D rb;
    7.     public float sidewaysForce = 500f;
    8.  
    9.     void FixedUpdate () {
    10.         if (Input.GetKey("d")) {
    11.             rb.AddForce(sidewaysForce * Time.deltaTime, 0);
    12.         }
    13.  
    14.         if (Input.GetKey("a")) {
    15.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0);
    16.         }
    17.     }
    18. }
    Your error is on line 11 of my version (line 16 of yours), and also line 15 (your 21). Let's look at the error messages:
    They're kinda both the same thing here, but one's telling you generally what's wrong, and the other is telling you specifically where the compiler is having issues.

    You're using the method AddForce, which belongs to the Rigidbody2D class. If you look up that method in the documentation (and, helpfully, in the first error message itself!), you'll see it requires one parameter of type Vector2, and can take an optional second parameter of type ForceMode2D).

    Look at your second error message. It's telling you that it can't convert your float type to Vector2. My guess is you're trying to pass just the X value in as the first parameter, and 0 in the second to have no Y value movement. The problem is that you forgot to wrap those values in a Vector2, so you're not actually doing what it is you think you're doing. If you do so, I bet it works for you (or at the very least, doesn't give you errors).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player_Move : MonoBehaviour {
    6.     public Rigidbody2D rb;
    7.     public float sidewaysForce = 500f;
    8.  
    9.     void FixedUpdate () {
    10.         if (Input.GetKey("d")) {
    11.             rb.AddForce(new Vector2(sidewaysForce * Time.deltaTime, 0));
    12.         }
    13.  
    14.         if (Input.GetKey("a")) {
    15.             rb.AddForce(new Vector2(-sidewaysForce * Time.deltaTime, 0));
    16.         }
    17.     }
    18. }
     
    Socks, Ryiah and SpongeBob135 like this.
  3. SpongeBob135

    SpongeBob135

    Joined:
    Dec 26, 2017
    Posts:
    8
    I replaced my code with yours, and it worked perfectly! Thank you for your help.
     
  4. Disastah

    Disastah

    Joined:
    Apr 12, 2017
    Posts:
    6
    Hey Schneider i want ask something

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player_Move : MonoBehaviour {
    6.  
    7.     public Rigidbody2D rb;
    8.  
    9.     public float sidewaysForce = 500f;
    10.  
    11.  
    12.     void FixedUpdate()
    13.     {
    14.  
    15.         if (Input.GetKey("d"))
    16.         {
    17.             rb.AddForce(Vector2.right * sidewaysForce * Time.deltaTime);
    18.         }
    19.  
    20.         if (Input.GetKey("a"))
    21.         {
    22.             rb.AddForce(Vector2.right * -sidewaysForce * Time.deltaTime);
    23.         }
    24.     }
    25. }
    26.  
    my code is working too, what's the difference your code with my code? which one is better?
     
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    The difference is with your new code, you're multiplying your sidewaysForce property by an actual Vector2, giving you a Vector2 in return. So this code works, while your first set of code did not.

    There's no functional difference between this and my code that I can see. And while there may be a difference in performance or efficiency (I'm honestly not aware if there is), it would be negligible at this state for you. Worry more about which is easier for you to read and maintain than which is more efficient. Only worry about optimizing stuff once you run into performance issues.

    Also, to actually tag someone for a response, use the @ symbol followed by the user's full name (like @Schneider21), or alternatively use the Reply function for their post. This will send them a forum alert so they know to come back and check the thread.
     
    Ryiah likes this.
  6. Disastah

    Disastah

    Joined:
    Apr 12, 2017
    Posts:
    6
    thank you for replying :)
     
  7. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Ha! I thought I was responding to the OP. Didn't notice someone new had joined the conversation.

    Whoopsie. :p
     
  8. SpongeBob135

    SpongeBob135

    Joined:
    Dec 26, 2017
    Posts:
    8
    I have a new problem....
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CoinCollect : MonoBehaviour {
    7.     public int score = 0;
    8.     public GameObject coin;
    9.     public Text ScoreText;
    10.  
    11.     void Update () {
    12.         ScoreText.text = score.ToString ();
    13.     }
    14.     // Update is called once per frame
    15.     void OnTriggerEnter () {
    16.         Destroy (gameObject, 0);
    17.         score = score + 1;
    18.     }
    19. }
    When I execute the code shown above and my character collides with it, it does nothing. I marked "Is Trigger" on the item's BoxCollider, but nothing still happens. If you can help me, that be real helpful.
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Generally speaking, when you have a new problem it's better to create a new thread. That way, the thread title and searches make some more sense.

    That being said, since I'm here anyways.. I believe your issue might be that you are using the 3D version of OnTriggerEnter instead of the 2D version. Try changing that & see what happens. It's worth mentioning that you may want to check the object with which the trigger collision has occured, to ensure that it is indeed the player object before destroying it, unless you are certain that nothing else can do so. =)

    Also note that once you destroy the coin in this example, you will lose access to the 'score' variable.
    Another note is that there is no point in setting the score in each call to the Update() method. It is much better to update the score text when your score changes (only then).
    With those 2 ideas in mind, it would be a good idea to have your score updated by a central script of sorts. That one script can have a method that is called to update the score variable, as well as the text. Your other "coins" and whatever else can reference said gameobject/script, and pass information to it.

    I hope that helps some. Good luck with your game :)
     
    Schneider21 and Socks like this.
  10. SpongeBob135

    SpongeBob135

    Joined:
    Dec 26, 2017
    Posts:
    8
    I tried it, but still nothing.
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Can you post the function you're using now?

    Small checklist:
    1) at least 1 of the objects interacting has a rigidbody, plus a collider
    2) collider set as trigger, as you said you already have.

    That should be enough.
     
  12. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    @SpongeBob135 Some more advice: "It's not working" or "It does nothing" is slightly above useless information for us. And for you, as well!

    Use Debug Logs liberally in code that isn't working how you expect. Example:
    Code (CSharp):
    1. Debug.Log("Some message here: " + yourVariableIfYouWant);
    Because here's the thing: The code always* does what you write. So it always "works". If it's not doing what you intend or expect, it's either written incorrectly or the logic is wrong. While that may seem pedantic, it should actually be encouraging; it means you will succeed if you stick it through, learn more, and figure out what you did wrong.

    If you want to get fancy, you can learn how to use breakpoints in Unity, too. This gives you access to even more information than you can get with Debug Logs, so it's worth learning.

    - - -

    * - Barring an actual bug in the engine, of course
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well said :)
     
  14. SpongeBob135

    SpongeBob135

    Joined:
    Dec 26, 2017
    Posts:
    8
    Nevermind, I found the problem. The coin and the player were on different layers. I aligned them, and everything went well. Thanks anyways for your help.
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, I'm glad that you got it resolved :)
     
  16. SpongeBob135

    SpongeBob135

    Joined:
    Dec 26, 2017
    Posts:
    8