Reputation: 3219
I am trying to create a set of buttons that manipulate the 2D game object, and all that people are posting about it InputManager, but I do not think that is what I am trying to do. I am looking for a c# code that will do the following: (and the code I have currently is posted below)
Update()
or FixedUpdate()
method that will check if the button is still pressed.My code as of right now just moves the object ONCE, not noticing the rest of the clicks after that.
I genuinely appreciate all help recieved, thank you.
using UnityEngine;
using System.Collections;
public class fScript : MonoBehaviour {
public GameObject player;
private Rigidbody2D rb;
Vector3 v;
// Use this for initialization
void Start () {
rb = player.gameObject.GetComponent<Rigidbody2D> ();
v = new Vector3 (10, 0, 0);
}
// Update is called once per frame
public void MovePLayer () {
rb.AddForce (v);
Debug.Log ("CLICKED");
}
}
Upvotes: 1
Views: 377
Reputation: 11427
In this case you need to try use to set Velocity. Velocity constantly slowsDown at every next moment. If you will change velocity every frame you will have constantrly speed when button is pressed. But one more thing. You need to apply not to OnClick event! You need OnPress event that is not realized in UI button, as I know. Try to fix this with next code:
private bool MouseIsDown;
void OnPointerDown(){
MouseIsDown = true;
}
void OnPointerUp(){
MouseIsDown = false;
}
and needed code with velocity inside of fixed update in IF body when MouseIsDown== true;
Upvotes: 0