- Home /
Rigidboy.velocity with integer?
I'm trying to do a movement mechanics similar to snake game: when i press a button, the player keeps moving in that direction in a constant velocity (in other words, a grid movement, or square by square movement).
I want to the player's position increase 1 for every second. (example: if i press the up button, the player's Y axis wil increase in 1 for every second)
I've tried two differents way:
GameObject.transform.Translate(0, 1 * Time.deltaTime, 0);
Rigidbody2D.velocity = new Vector2(0, 1);
But they give the same output.
Can someone please show me how can i do that?
Thnks!
Answer by Meishin · Aug 20, 2019 at 03:29 AM
You're very close to it, it's just that in Update/LateUpdate the code will be called every frame / Fixed timeframe. You can just tweak it a little, for example using a co routine called every 1 second;
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
void Start()
{
StartCoroutine (onCoroutine());
}
IEnumerator onCoroutine()
{
while(true)
{
// Add Movement orders here, for example
GameObject.transform.Translate(0, 1f, 0);
yield return new WaitForSeconds(1f);
}
}
}
I guess you could play on FixedFrame time but the player commands would then be squetchy (Well i guess it could be used for some specific gameplay style). For the above code, place player commands in an Update function :)