Object moves randomly when jumping

I created this simple C# movement script:

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour
{
public float speed = 2.5f;
public float jumpspeed = 5.0f;

void Update()
{
if (Input.GetKey(KeyCode.A))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.W))
{
transform.position += Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
transform.position += Vector3.back * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.Space))
{
transform.position += Vector3.up * jumpspeed * Time.deltaTime;
}
}
}

But when i start jumping the object randomly starts moving and after like 5 jumps it starts moving very fast

Please refer to the sticky post regarding code tags.

You may want to change your jump logic to GetKeyDown instead…

if (Input.GetKeyDown(KeyCode.Space)){
    //jump logic
}

Does your player have a rigidbody or character controller? If so, the “random movement” you’re seeing might be the result of the effect of gravity fighting your moving upwards. Your code just moves the character up at a constant speed, but his downward momentum will be continuously increasing while you’re not touching the ground, so these two will be fighting each other before long. And because physics updates and Update() don’t happen at the same time, this is probably going to look weird and flickery.

It has a rigidbody, but when i change it to a character controller and i go up the player just goes up and doesn’t fall down

If you are going to use gravity, I’d suggest adding a force to create jumping.

Otherwise, how about using translation for your jumping and disabling gravity for the rigidbody while in the air, re-enabling it upon being grounded again.

Ok, i’ll try both of those. Thank you!