OK, in your code you set the velocity only on keydown, so then if the rigidbody experiences friction it will slow to a stop.
If you want to ensure constant velocity per frame (when key pressed) then something like below should do it.
No need to scale by Time.deltaTime in this case, physics engine will do that. But if you were translating (without physics) in update then yes… scale by deltaTime.
Update gets called once per frame, but to ensure smooth move you should use FixedUpdate; that gets called once per physics engine iteration.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveSphere : MonoBehaviour
{
Rigidbody2D rb;
public int movespeed;
private Vector2 vVelocity = Vector2.zero;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per physics frame
void FixedUpdate()
{
if(rb)
{
rb.velocity = vVelocity;
}
}
public void rightButtonDown()
{
vVelocity.x = movespeed;
}
public void buttonsUp()
{
vVelocity.x = 0;
}
}
thanks i have fixed it i realised that the new vector for the velocity in the why access was 0 for some reason it messed with the gravity no ideawhy but ive got this now
if (Input.GetKey(KeyCode.RightArrow))
{
rb.velocity = new Vector2(runspeed * Time.deltaTime, rb.velocity.y);
anime.SetInteger(“MoveCode”, 1);
}
else
{
rb.velocity = new Vector2(0, rb.velocity.y);
}