So I’m a bit new to programming and I want to make my character (a cube for now) to walk 1 unit per click on a button that will be on the screen.
Also if possible how can I for example: if I press 2 times this button he jumps or if I press 2 times this button + 1 time other button he attack, etc.
It will be a 2D game btw.
Thanks.
Can the cube walk in only 1 direction ? ![]()
It is possible to have a mouse click = movement and 2 mouse clicks (received within a time frame you choose, count as a double click = jump).
It will walk to the right and left only.
And yeah that will do I guess, because it’s just a prototype for university, but I don’t know how to build the full code ;-;
I’m trying to do a rhythm game, where you need to press buttons on the corners of the screen to do every action on the character, acording to the music.
I mean , you could always use a different key to jump, optionally, instead of a double-click. These are just options/ideas.
Try looking up “MoveTowards” in the Unity docs. If you are using physics, try velocity instead.
One step at a time ![]()
Attach this script to the Cube
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveObject : MonoBehaviour {
public float speed = 5f;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.D))
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
}
}
This will move the cube right and left each single click on A and D keys.
Oh thanks for writing it! ^^