(Unity) Rapidly click to speed up an object (otherwise decelerate)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



(Unity) Rapidly click to speed up an object (otherwise decelerate)



I’m trying to have a button that when you click it, cause the speed to increase incrementally for each time you click. When you stop clicking, it begins to slow down. I’ve tried various methods from rigidbody.velocity (didn’t work because object was kinematic), to transform.Translate, to transform.MovePosition. Any help would be appreciated, as I need to figure this out fast.



EDIT: The button being clicked is a sprite.


void OnMouseDown()
clicked = true;


void ifClicked()
if (clicked)
speed += 0.5f;
gameObject.transform.Translate(Vector3.right * speed * Time.deltaTime);
StartCoroutine("Decelerate");



IEnumerator Decelerate()
yield return new WaitForSeconds(1);
speed -= 0.5f;





Could you show us the code for it that you have now?
– Ryolu
1 hour ago





Unfortunately, my laptop’s battery just went out. I posted this from my phone.
– Kat K
1 hour ago





Alright, what does it do now then?
– Ryolu
1 hour ago





Nothing, unfortunately. I don’t have a good enough grasp on methods that make objects move. What worked best was using transform.Translate, but it made for very choppy movement, which wasn’t what I was going for.
– Kat K
45 mins ago





1 Answer
1



You could have a float which will increase every time you click that button(like a multiplier) and the transform.position of that particular gameobject could be translated with that multiplier.


transform.position


// A button could call this function from OnClick()
public void Multiply()

// where multiplier is a member variable of the class you're using this in
multiplier *= 1.1f;


// And add these into Update()
transform.position += movementVector * multiplier;
multiplier *=0.95f;





It's probably really flawed since its 7 in the morning and I haven't fully woken up yet
– Ryolu
14 mins ago





Unity doesn't seem to like that. It's giving me an error message that says "Operator '*=' cannot be applied to operands of type 'Vector3' and 'Vector3'. Note, I did add the line "Vector3 movementVector;" to my list of variables.
– Kat K
11 mins ago





Sorry, I made a typo. The edit should fix it
– Ryolu
6 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS