Question: I've made a nice code that works but why does my character keep floating away instead of moving and staying in one place this is

I've made a nice code that works but why does my character keep floating away instead of moving and staying in one place this is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
const float speed =1.0f;
Vector3 velocity;
// Start is called before the first frame update
void Start()
{
velocity = speed * new Vector3(1.0f,1.0f,0.0f);
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("w"))
{
velocity = new Vector3(velocity.x,1* speed, 0.0f);
}
else if (Input.GetKey("a"))
{
velocity = new Vector3(-1* speed, velocity.y,0.0f);
}
else if (Input.GetKey("s"))
{
velocity = new Vector3(velocity.x,-1* speed, 0.0f);
}
else if (Input.GetKey("d"))
{
velocity = new Vector3(1* speed, velocity.y,0.0f);
}
transform.position = transform.position +(velocity * Time.deltaTime);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!