0
\$\begingroup\$

Pretty simple, have a rotate up button and I want it to rotate while held down.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class cannonController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
public Vector3  force;
Rigidbody2D rg;
Vector3 up;
public bool pointerDown = true;
Vector3 down; 
// Use this for initialization
void Start () {
    rg = GetComponent<Rigidbody2D>();
    up = new Vector3(0f, 0f, 20f);
    down = new Vector3(0f, 0f, -3f);
}

/// <summary>
///  all of the rotation stuff
/// </summary>
    public void RotateSTOP()
    {
        rg.transform.Rotate(Vector3.zero);
    }
    public void RotateDown()
    {
    rg.transform.Rotate(down);
    }
    public void RotateUp()
    {
    while(pointerDown == true)
    {
        rg.transform.Rotate(up, Time.deltaTime);
    }

    }
/// <summary>
/// sets pointerDown to check if button is held down
/// </summary>
/// <param name="eventData">Event data.</param>
public void OnPointerDown(PointerEventData eventData)
{
    pointerDown = true;
}

public void OnPointerUp(PointerEventData eventData)
{
    pointerDown = false;
}

}

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

Well, all the above answers were already correct so far but here is my example with code. It's a basic but working implementation. You will have to adjust it to your needs.

For the sake of completeness here is how (someone needs) to configure the buttons

  • attach the script to the canon
  • First create two ui buttons
  • add an Event Trigger component on each
  • map On Pointer Down and On Pointer Up of each button to call the according methods on the bottom of the script


public class CanonController : MonoBehaviour {

public GameObject goToRotate;

public Vector3 up = new Vector3(0f, 0f, 20f);
public Vector3 down = new Vector3(0f, 0f, -20f);

// public just for the reason to start it within the inspector
public bool isUpDown;
public bool isDownDown;

// Use this for initialization
void Start () {
    isUpDown = false;
    isDownDown = false;

    if (goToRotate == null) goToRotate = this.gameObject;
}

void Update () {
    // I leave it up to you to decide between Update and FixedUpdate
    if (isUpDown) goToRotate.transform.Rotate(up * Time.deltaTime);
    if (isDownDown) goToRotate.transform.Rotate(down * Time.deltaTime);
}

//those methods are called from the EventTrigger component of the button up and button down
//use Pointer Up and Pointer Down and match them accordingly
public void UpDown()  { isUpDown = true; }
public void UpUp()    { isUpDown = false; }
public void DownUp()   { isDownDown = false; }
public void DownDown() { isDownDown = true; }

}

and it should rotate

if both buttons are pressed at the same time it gets rotated twice and if up and down are the exact opposite it should end up with zero rotation.


A small addition to the code given by you

 
    public void RotateUp()
    {
        while(pointerDown == true)
        {
            rg.transform.Rotate(up, Time.deltaTime);
        }
    }

This can lead into an (game freezing) endless loop if you would call it in OnClick since there is no way single threaded Unity could set pointerDown to false somewhere else.

\$\endgroup\$
0
\$\begingroup\$

You need to call the methods to make them work, simply putting them in assembly not helps.

void Update()
{
    if(pointerDown == true)
    {
        rg.transform.Rotate(up, Time.deltaTime);
    }    
}
\$\endgroup\$
2
  • \$\begingroup\$ How would it then differentiate between using RotateUP() and RotateDOWN()? And copy pasting that didnt seem to work when hold down still yet. Clicking over and over does \$\endgroup\$ Mar 13, 2016 at 16:39
  • \$\begingroup\$ imgur.com/5bTyPa0 \$\endgroup\$ Mar 13, 2016 at 16:42
0
\$\begingroup\$

you could try something like this

void FixedUpdate()
{
    if(pointerDown == true)
    {
        rg.transform.Rotate(up, Time.deltaTime);
    }
    else
    {
        rg.transform.Rotate(down, Time.deltaTime);
    }
    rg.transform.rotation = Mathf.Clamp(rg.transform.rotation, down, up);
}

in this way the cnnon should rotate up while helding the button and down when button is released.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Why FixedUpdate? \$\endgroup\$
    – Mike Hunt
    Mar 13, 2016 at 22:18
  • \$\begingroup\$ That seems like an iffy way to do it but man what do I know. Just thought there should be some specific, scalable way to by default. Not sure how youd do this if there were 3 dimensions but ill just worry about solutions xD and dont you use FixedUpdate everytime its physics related, @MikeHunt \$\endgroup\$ Mar 13, 2016 at 22:27
  • \$\begingroup\$ @AdamSavages Maybe it's because it's almost midnight here, but I don't understand what you're trying to say lol... You are correct in saying that FixedUpdate should be used whenever you're using the physics engine... Unfortunately, rg.transform is basically saying gameObject.transform, meaning you aren't actually using the rigidbody or physics engine at all. \$\endgroup\$
    – Mike Hunt
    Mar 13, 2016 at 22:46
  • 1
    \$\begingroup\$ @AdamSavages This operation is not physics related. You are manipulating a transform here, so this should be done in Update method. \$\endgroup\$ Mar 14, 2016 at 14:02
  • 1
    \$\begingroup\$ @AdamSavages I answered at work. I just used parts of the code you used in your question. This is not by far how I would handle it. Since you lacked details I tried to sort out something to put your code to work. If you're looking for alternate solutions, I think you should detail your goal, give a context and be as specific as you can. \$\endgroup\$
    – Leggy7
    Mar 14, 2016 at 23:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .