[script attached] Simple flying script

Hello, I am trying to remake an FPS Movement Controller script for a flying script. So that the gravity will have no impact at all and the player will be able to go up and down.

I created a camera, attached it in front of the player controller, but the script doesn’t work as it should: if I press “A” - it goes to the right, if I press “D” - it also goes to the right. Same for the “W” and “S”.

Could you please suggest what is wrong with the script?

using UnityEngine;
using System.Collections;

public class watermovement : MonoBehaviour {

	// This component is only enabled for "my player" (i.e. the character belonging to the local client machine).
	// Remote players figures will be moved by a NetworkCharacter, which is also responsible for sending "my player's"
	//location to other computers.
	
	public float waterspeed = 10f;		// The speed at which I run
	public float waterjumpSpeed = 6f;	// How much power we put into our jump. Change this to jump higher.
	
	// Booking variables
	Vector3 direction = Vector3.zero;	// forward/back & left/right
	float   verticalVelocity = 0;		// up/down
	
	CharacterController cc;
	Animator anim;

	public Camera thecam;


	// Use this for initialization
	void Start () {
		cc = GetComponent<CharacterController>();
		anim = GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {
		
		// WASD forward/back & left/right movement is stored in "direction"
//		direction = transform.rotation * new Vector3( Input.GetAxis("Horizontal") ,0, Input.GetAxis("Vertical") );
//		direction = thecam.transform.forward;

//			if (Input.GetAxis("Vertical") != 0)
//			{
//		//		transform.Translate(Vector3.forward * waterspeed * Input.GetAxis("Vertical"));
//				transform.Translate(thecam.transform.forward * waterspeed * Input.GetAxis("Vertical"));
//				
//			}


		if (Input.GetKey(KeyCode.W)) {
			
			transform.Translate(thecam.transform.forward * waterspeed * Input.GetAxis("Vertical"));
		}
	
		if (Input.GetKey(KeyCode.S)) {
			
			transform.Translate(-thecam.transform.forward * waterspeed * Input.GetAxis("Vertical"));
		}
			


		if (Input.GetKey(KeyCode.A)) {
				transform.Translate(Vector3.left * waterspeed * Input.GetAxis("Horizontal"));
		}

		if (Input.GetKey (KeyCode.D)) {
				transform.Translate(Vector3.right * waterspeed * Input.GetAxis("Horizontal"));
		}

//			if (Input.GetAxis("Horizontal") != 0)
//			{
//				transform.Translate(Vector3.right * waterspeed * Input.GetAxis("Horizontal"));
//		//	transform.Translate(thecam.transform.right * waterspeed * Input.GetAxis("Vertical"));
//		}



		// This ensures that we don't move faster going diagonally
		if(direction.magnitude > 1f) {
			direction = direction.normalized;
		}

	}

	
	// FixedUpdate is called once per physics loop
	// Do all MOVEMENT and other physics stuff here.
	void FixedUpdate () {
		
		// "direction" is the desired movement direction, based on our player's input
		Vector3 dist = direction * waterspeed * Time.deltaTime;

		// Apply the movement to our character controller (which handles collisions for us)
		cc.Move( dist );
	}
}

Thanks!

What’s happening with your horizontal motion is: when you press ‘A’, the GetAxis() function is returning a negative number. You then multiply this by Vector3.left which is (-1,0,0) and the result is a positive number which is the same as if you had just pressed ‘D’. If you change the code as follows, you should be able to move left and right.

 if (Input.GetKey (KeyCode.D) || Input.GetKey(KeyCode.A)) {
         transform.Translate(Vector3.right * waterspeed * Input.GetAxis("Horizontal"));
 }

As for the vertical direction, I’m not sure if about the answer since it appears you’re using the Vector3.forward direction as your vertical direction? I would have asssumed it would be Vector3.up, in which case the solution would be the same as above.

For the LEFT/RIGHT problem, I would focus on lines 57 and 61, for example:

 transform.Translate(Vector3.left * waterspeed * Input.GetAxis("Horizontal"));

The Input.GetAxis(“Horizontal”) will have a sign (positive or negativate) already. So that will handle the LEFT/RIGHT for you. So both lines should use the same direction: Like “Vector3.left”. Otherwise you’re double flipping the direction and you’ll always go RIGHT regardless of whether D or A is clicked.

Thank you guys for answers! These answers really helped. I also wanted to say that the community here, at Unity is just wonderful - patient and eager to assist. I added a comment with a working script to the main question. Thank you LazyElephant and jmonasterio for help, - I upvoted both of your answers.