AddForce does not move object when keys are pressed

Hey guys,

I’m trying to set up some basic player movement using the physics engine.

The idea is that they player moves forward or backward at a given speed, which can be adjusted with the W and S keys. They should also be able to move left and right using A and D, but it should not be continuously applied.

However, when the buttons are pressed, the object that the script is applied to does not move. I’ve been banging my head against this for a few hours, and any help/pointers/suggestions would be hugely appreciated. Thanks!

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

	public Vector3 flightspeed;
	public float thrust = 5;
	public float maneuver = 50;
	public GameObject player = GameObject.FindWithTag ("Player");

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
		if(Input.GetKeyDown(KeyCode.W))
		{
			flightspeed.y = flightspeed.y + thrust;
		}
		if(Input.GetKeyDown(KeyCode.S))
		{
			flightspeed.y = flightspeed.y - thrust;
		}
		if(Input.GetKeyDown(KeyCode.A))
		{
			rigidbody.AddForce(Vector3.left * maneuver, ForceMode.Impulse);
		}
		if(Input.GetKeyDown(KeyCode.A))
		{
			rigidbody.AddForce(Vector3.right * maneuver, ForceMode.Impulse);
		}

	}

	void FixedUpdate ()
	{
		rigidbody.AddForce(flightspeed);
	}

}

Looks like you’re adding force to the rigidbody of the Movement script.

I’m guessing you meant to add it to the local variable player object instead?

Well, I came back to the problem with fresh eyes and figured it out, and now I feel dumb.

Problem 1: I had “Is Kinematic” checked, which makes forces have no effect on the object.

Problem 2: I had the A key set to add equal force in opposite directions, negating any effect. (Great job!)

Thanks again alfalfasprout for the suggestion.