problem in my work need help.

hi iam doing my first work in unity and iam in need help to work it out.

in my work my player has to move in up and side directions according to the respective keys pressed (i.e) W,A,D and my player has to decline down if no keys are pressed.

the problem is when i press the up direction it is moving up but when i press the left and right keys the player declines down in sideways.

i did figured it out what was the problem but i dont know how to over it. or is there any other way to do this. help me out.

the script is below

using UnityEngine;
using System.Collections;

public class player_script : MonoBehaviour {
	
	//public static float x;
	//public static float y;
	float accleration = 5;
	Vector3 move;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		if(Input.GetAxis ("Horizontal")!=0)
		{
		   move.x = Input.GetAxis ("Horizontal");
		}
		else
		{
			move.x = 0;
		}
		
		if(Input.GetAxis ("Vertical")!=0)
		{
			move.y = Input.GetAxis ("Vertical");
		}		
		else
		{
			if(transform.position.y >= -4.45f)
			{
				move.y = -2;
			}
		else
		{
			move.y = 0;
		}
		}
		
		move.Normalize();
		
		float xdir = move.x * accleration * Time.deltaTime;
		float ydir = move.y * accleration * Time.deltaTime;
		
		transform.position = new Vector3(transform.position.x+xdir,transform.position.y+ydir,transform.position.z);
			
	}
}

Ok, well i’ll start of by making a few amendments to your current script. Where you have written things like:

 if(Input.GetAxis ("Horizontal")!=0)
       {
          move.x = Input.GetAxis ("Horizontal");
       }
       else
       {
         move.x = 0;
       }

could be written in just one line of code.

move.x = Input.GetAxis ("Horizontal");

By calling Input.GetAxis() in your code, it will return a float between 1, or 0, so those values dont need to be set manually.

Now, you say you want the player to only move down if no keys are pressed. So all you need to do is create an if statement checking to see if all input.GetAxis() are equal to 0. Your new code should look something like:

using UnityEngine;
using System.Collections;
 
public class player_script : MonoBehaviour {
 
    //public static float x;
    //public static float y;
    float accleration = 5;
    Vector3 move;
 
    // Use this for initialization
    void Start () {
 
    }
 
    // Update is called once per frame
    void Update () {
		//Set movement values to 0.0f - 1.0f
		move.y = Input.GetAxis ("Vertical");
		move.x = Input.GetAxis("Horizontal");
		
		//Check if no buttons are pressed
		if(Input.GetAxis("Vertical") == 0f && Input.GetAxis("Horizontal") == 0f) {
			if(transform.position.y > -4.45f) {
				move.y -= 2f;	
			}
		}
		
       	move.Normalize();
 
       	float xdir = move.x * accleration * Time.deltaTime;
       	float ydir = move.y * accleration * Time.deltaTime;
 
       	transform.position = new Vector3(transform.position.x + xdir, transform.position.y + ydir, transform.position.z);
    }
}

your player is always going to move down until it reaches -4.45f on the Y axis if this line is in there

if(transform.position.y > -4.45f) {
          move.y -= 2f;   }

the other possibility is that you have attached a rigidbody to your player, in which case gravity will be pulling the player down (unless it’s unchecked in the editor)

as another programming tip :
where you have -4.45f and 2f you may be better off setting variables in your declarations such as

float gravity = 2f;
float floor = -4.45f;

then your line could read

if(transform.position.y > floor) {
              move.y -= gravity;   }

you could go one step further and expose these to the editor so that you can tweak them during runtime

public float gravity = 2f;
public float floor = -4.45f;