Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't get player object to move

Discussion in '2D' started by Bloodember, Apr 22, 2014.

  1. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    I'm having problems getting my player obj. to move. I'm making an infinite runner game, got the script written, but I get two errors, not sure what is wrong, here's the two errors
    1. (24,45)error CS1501: No overload for method `AddForce' takes `2' arguments
    2. (37,37)error CS1501: No overload for method `AddForce' takes `4' arguments

    Here's my script
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerScript : MonoBehaviour {
    6.  
    7.     public static float distanceTraveled;
    8.  
    9.     public float acceleration;
    10.     public Vector3 jumpVelocity;
    11.        
    12.     private bool touchingPlatform;
    13.     //private Vector3 startPosition;
    14.    
    15.     //void Start () {
    16.         //startPosition = transform.localPosition;
    17.         //renderer.enabled = false;
    18.         //rigidbody.isKinematic = true;
    19.         //enabled = false;
    20.     //}
    21.    
    22.     void Update () {
    23.         if(Input.GetButtonDown("Jump")){
    24.             if(touchingPlatform){
    25.                 rigidbody2D.AddForce(jumpVelocity, ForceMode.VelocityChange);
    26.                 touchingPlatform = false;
    27.             }
    28.  
    29.         }
    30.         distanceTraveled = transform.localPosition.x;
    31.  
    32.        
    33.  
    34.     }
    35.    
    36.     void FixedUpdate () {
    37.         if(touchingPlatform){
    38.             rigidbody2D.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
    39.         }
    40.     }
    41.    
    42.     void OnCollisionEnter () {
    43.         touchingPlatform = true;
    44.     }
    45.    
    46.     void OnCollisionExit () {
    47.         touchingPlatform = false;
    48.     }
    49.    
    50.  
    51. }
    52.  
    What I'm trying to do is get the player obj to run and jump, with a double jump. The running has to be always going, and jumping with a button press.
    Thanks for the help.
    I apologize if this has already been asked, I did a search and didn't find any answers.
     
  2. Wibber

    Wibber

    Joined:
    Dec 27, 2012
    Posts:
    12
    You are using Rigidbody.AddForce not Rigidbody2D.AddForce.
    ScriptReference
     
  3. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    Ok, I read the scriptReference and I took out one of the 0f, and other things and I still get the same errors, I'm not sure how to get it to work. Help would be appreciated.

    Thanks.
     
  4. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    Learn to understand what your compiler is trying to tell you. The error message says it all, really.

    Rigidbody2D.AddForce() only takes one parameter, the force. It doesn't take a force mode as Rigidbody.AddForce() does. (Well, at least not yet. Hopefully this will be a feature in a future release.)

    Long story short: Rigidbody2D.AddForce != Rigidbody.AddForce
     
    Last edited: Apr 23, 2014
  5. Wibber

    Wibber

    Joined:
    Dec 27, 2012
    Posts:
    12
    The Method takes only one parameter, you are trying to overload it with more than one parameter.

    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.      
    5.     public class PlayerScript : MonoBehaviour {
    6.      
    7.         public static float distanceTraveled;
    8.      
    9.         public float acceleration;
    10.         public Vector3 jumpVelocity;
    11.            
    12.         private bool touchingPlatform;
    13.         //private Vector3 startPosition;
    14.        
    15.         //void Start () {
    16.             //startPosition = transform.localPosition;
    17.             //renderer.enabled = false;
    18.             //rigidbody.isKinematic = true;
    19.             //enabled = false;
    20.         //}
    21.        
    22.         void Update () {
    23.             if(Input.GetButtonDown("Jump")){
    24.                 if(touchingPlatform){
    25.                     rigidbody2D.AddForce(jumpVelocity);
    26.                     touchingPlatform = false;
    27.                 }
    28.      
    29.             }
    30.             distanceTraveled = transform.localPosition.x;
    31.      
    32.            
    33.      
    34.         }
    35.        
    36.         void FixedUpdate () {
    37.             if(touchingPlatform){
    38.                 rigidbody2D.AddForce(acceleration);
    39.             }
    40.         }
    41.        
    42.         void OnCollisionEnter () {
    43.             touchingPlatform = true;
    44.         }
    45.        
    46.         void OnCollisionExit () {
    47.             touchingPlatform = false;
    48.         }
    49.        
    50.      
    51.     }
    52.  
     
  6. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    Thanks for both of your help, I wasn't understanding what it was telling me. My errors are gone, now the player just falls off the screen when I hit play.

    Here's what my code now.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerScript : MonoBehaviour {
    6.  
    7.     public static float distanceTraveled;
    8.  
    9.     public Vector2 acceleration;
    10.     public Vector3 jumpVelocity;
    11.        
    12.     private bool touchingPlatform;
    13.     //private Vector3 startPosition;
    14.    
    15.     //void Start () {
    16.         //startPosition = transform.localPosition;
    17.         //renderer.enabled = false;
    18.         //rigidbody.isKinematic = true;
    19.         //enabled = false;
    20.     //}
    21.    
    22.     void Update () {
    23.         if(Input.GetButtonDown("Jump")){
    24.             if(touchingPlatform){
    25.                 rigidbody2D.AddForce(jumpVelocity);
    26.                 touchingPlatform = false;
    27.             }
    28.  
    29.     }
    30.         distanceTraveled = transform.localPosition.x;
    31.  
    32.        
    33.  
    34.     }
    35.    
    36.     void FixedUpdate () {
    37.         if(touchingPlatform){
    38.             rigidbody2D.AddForce(acceleration);
    39.             //rigidbody2D.velocity = new Vector2(1f, rigidbody2D.velocity.y);
    40.         }
    41.     }
    42.    
    43.     void OnCollisionEnter () {
    44.         touchingPlatform = true;
    45.     }
    46.    
    47.     void OnCollisionExit () {
    48.         touchingPlatform = false;
    49.     }
    50.    
    51.  
    52. }
    53.  
    When I turn Kinematic on for the Rigidbody2d he doesn't fall, just stands there with no movement, should this be on or off?

    I'm slowly figuring this out.
     
    Last edited: Apr 23, 2014
  7. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    Let me quote what your bible says about Rigidbody2D.isKinematic:
    If this property is set to true then the rigidbody will stop reacting to collisions and applied forces.