Applying Force in 2D and calling a function

Okay so i watched the 2d platformer tutorial and followed along then redid it without the video to get used to what to do now im trying to set it up myself from scratch.

Ive hit a snag with the C# I want to use set keys instead of input axis with the current setup(see below) after going through the flip function over and over trying it throughtout the code in segments i cannot get it to apply without calling it as a function but there is my second snag i thought dropping the addforce and flip into a function together would allow me to do both on keypress whereas the current way only flips and if flip isnt there it adds the force. Can anyone point me towards why?

 using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	public Rigidbody2D rg2d;
	public float movespeed = 10f;
	public bool facingRight = true;

	void Start () {	
	}
	
	void FixedUpdate () {
		rg2d = GetComponent  ();
		if ((Input.GetKey (KeyCode.D)) && !facingRight)
			moveright ();
		if ((Input.GetKey (KeyCode.A)) && facingRight)
			moveleft ();
	}
	void Flip(){
		facingRight = !facingRight;
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
	}

	void moveright(){
		rg2d.AddForce (new Vector2 (movespeed, 0f));
		Flip ();
	}

	void moveleft(){
		rg2d.AddForce (new Vector2 (-movespeed, 0f));
		Flip ();
	}
		                  }
`

`

First off, it’s considered bad practice with things such as player movement to use KeyCode instead of GetAxis becuase the axis are remapable. You really only want to use KeyCode for things like pressing Escape to bring up pause menus.

You’re assigning rg2d wrong. If you want to get components on the current gameobject you use:

rg2d = gameObject.GetComponent<Rigidbody2d>();

gameObject is different than GameObject. Basically, GameObject is an overarching class. gameObject is the current instance.

The logic in your script is as follows:

  1. If user has pressed ‘D’ key AND we aren’t facing right, then move right.
  2. If user has pressed ‘A’ key AND we are facing right, then move left.

Doesn’t make much sense does it? We want to be able to move in any direction at anytime (I’m assuming that’s what you want). From what I recall, you want to do the flip if you’re facing the opposite direction to where you plan to go. The logic you want is:

  1. If user has pressed ‘D’ key, check to see if we’re facing right. If we aren’t, then flip. Either way we still want to move.

  2. If user has pressed ‘A’ key, check to see if we’re facing left. If we aren’t, then flip. Either way we still want to move.

You’re better off nesting your if’s.

if(Input.GetKey(KeyCode.D))
{
    if(facingRight == false)
    {
        Flip();
    }

    moveRight();
}

if(Input.GetKey(KeyCode.A))
{
    if(facingRight == true)
    {
        Flip();
    }

    moveLeft();
}

Notice that the !facingRight is gone. !something can mean a couple things. The exclamation point means NOT. In some languages calling !something can return either the opposite value, 0, null, nil, or whatever weird stuff the language does that means 0 or the opposite. Just be explicit and use true or false.

Take Flip() out of moveRight() and moveLeft(). You actually don’t even need those functions there but if you plan on doing something else with movement then keep them as functions.