Could use some help in regards to problems with the arrow keys/WASD

I’m currently following the basic Roll-A-Ball tutorial and have run into a problem with the movement controls. Whenever I use the arrow keys or WASD to try and move my sphere, nothing happens. I can’t see any obvious flaws in my script so could someone please help me out a bit.

The script is here just in case:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
		public float speed;

		void fixedupdate ()
		{
				float moveHorizontal = Input.GetAxis ("Horizontal");
				float moveVertical = Input.GetAxis ("Vertical");

				Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); 

				rigidbody.AddForce (movement * speed * Time.deltaTime);
		}
}

Any help would be greatly appreciated!

Heya,

First of all, you’re kinda redundant by putting it into FixedUpdate() and using Time.deltaTime for calculating, as FixedUpdate() gets called at a fixed time (0.02 secs for example), while Update() is called per frame, which is obviously not at a stable rate. It’s more advised to move movement in the Update() method, so you are fine with that Time.deltaTime.

For your actual problem, it seems to me like rigidbody.AddForce() uses normalized values, to say 0 to 1. You can translate those values by dividing them this way:

 void fixedupdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal") / Screen.width;
float moveVertical = Input.GetAxis ("Vertical") / Screen.height;
 
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
rigidbody.AddForce (movement * speed * Time.deltaTime);
}

Here is another attempt that might work, didn’t try.

void fixedupdate ()
	{
		float moveHorizontal = Input.mousePosition.x * speed * Time.deltaTime;
		float moveVertical = Input.mousePosition.y * speed * Time.deltaTime;
		
		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
		rigidbody.transform.position = movement;
	}

No guarantee, but I hope I can help

Your problem should be solved by calculating the moveHorizontal and moveVertical to normalized values first (ranging from 0 to 1). Divide them by Screen.width and Screen.height, that should be the issue as AddForce() uses normalized values.

Cheers