Search Unity

Vector2.up acts weird and diffrent to Vector2.right?

Discussion in 'Physics' started by ThatsMaik, Nov 11, 2015.

  1. ThatsMaik

    ThatsMaik

    Joined:
    Oct 18, 2015
    Posts:
    4
    Hello Unity Community,

    My first post, guess im right here. :)

    So i have this script for my player movement. My problem is that the player accelerates really fast to an infinite speed when pressing W and S (vertical movement) and stays in an moderate constant speed when moving horizontally with A and D. So it seems their must be some kind of diffrents between the Vector2.up and Vector.right expression. I can't see anything wrong with my script. The player has a Rigidbody2D with Mass 1 and 0 Gravity.
    What am i doing wrong here?

    My PlayerMovement C#-Script Script:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     bool flipped;
    9.  
    10.  
    11.     void Setup() {
    12.         flipped = false;
    13.     }
    14.  
    15.  
    16.     void FixedUpdate () {
    17.  
    18.      
    19.         if (Input.GetKey (KeyCode.D)) {
    20.  
    21.             GetComponent<Rigidbody2D>().AddForce(Vector2.right * speed);
    22.  
    23.             if(flipped == true) {
    24.                 transform.localScale += new Vector3(2, 0, 0);
    25.                 flipped = false;
    26.             }
    27.      
    28.         } else if (Input.GetKey (KeyCode.A)) {
    29.  
    30.             GetComponent<Rigidbody2D>().AddForce(-Vector2.right * speed);
    31.  
    32.             if(flipped == false) {
    33.                 transform.localScale -= new Vector3(2, 0, 0);
    34.                 flipped = true;
    35.             }
    36.          
    37.         }
    38.  
    39.         if (Input.GetKey (KeyCode.W)) {
    40.  
    41.             GetComponent<Rigidbody2D>().AddForce(Vector2.up * speed);
    42.  
    43.          
    44.         } else if (Input.GetKey (KeyCode.S)) {
    45.  
    46.             GetComponent<Rigidbody2D>().AddForce(-Vector2.up * speed);
    47.              
    48.         }
    49.     }
    50. }
    51.  

    Thanks for helping,...
     
    Last edited: Nov 11, 2015
  2. DWilliams

    DWilliams

    Joined:
    Jan 12, 2015
    Posts:
    63
    By the looks of your script, it should cause the ever increasing to infinite speed with ALL the WSAD keys. Not sure why it's doing what it's doing with the constant speed horizontal movement. Some things 'wrong' with the script that come to mind:

    Your input checking should be in Update not FixedUpdate, but you should put your addforce stuff in FixedUpdate, so you'd have to set some variables to represent key presses within Update and use those variables in FixedUpdate to do the actual force stuff.

    You'll probably want to cache the rigidbody2d so you're not doing getcomponent calls each fixedupdate loop.
     
  3. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Dwilliams, why not just store the force n a Vector3, and then in fixedUpdate() ad the force?