I am working on block breaker & I have a kinda simple question. How can I make it so that my movement speed increases if I hold a button? Like if I hold X on the controller while moving the paddle would move faster?
…public class MoveLR : MonoBehaviour
{
public float PlayerSpeed = 5;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float amtToMove = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
transform.Translate(Vector3.right * amtToMove);
}
}…
I have that to move the paddle with the gamepad instead of the mouse.
Hi David,
In the Update method, you could add Time.deltaTime
to the value of your speed variable. Since your idea is to make this happen if a key is pressed down, you could wrap the code in an if-statement. If you do not know yet what this is or find this too challenging, I would recommend to complete the next few sections. After you completed the course, you should have learnt enough to implement that feature in your game.
Also please feel free to ask our helpful community of students over on our Discord chat server.
I hope this helped.
See also:
- Forum User Guides : How to apply code formatting within your post
- Forum User Guides : How to mark a topic as solved
TY I’ll do that today. I completed the course like 3 years ago but I don’t remember everything. I don’t do much coding I ended up paying someone to do 80% of my codes for the game while I do the building.
…{
public float PlayerSpeed = 5;
bool mofe = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float amtToMove = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
transform.Translate(Vector3.right * amtToMove);
}
private void Mofast()
{
if (CrossPlatformInputManager.GetButtonDown("Fire1"))
{
PlayerSpeed = 20f;
}
}
}
/* mofe = PlayerSpeed = 20*/…
That’s where I’m at sofar.