- Home /
The question is answered, right answer was accepted
How can i make GetButton only fire once?
enter code here
Okay so basically, i have 2 objects, both with cameras, and right now im using Input.GetButton("Fire1") to check to see if one of the objects was clicked. It works but when im pressing mouse button its reading getbutton like 4 or 5 times resulting in weird behavior. so my question is, how an i make it so that this betbutton call will only return once per click? GetButtonDown("Fire1") does not work.
using UnityEngine;
using System.Collections;
public class CameraControl : MonoBehaviour {
public Transform target;
public Vector3 position;
public float rotateSpeed = .01f;
public float distance = 2.8f;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -80;
public float yMaxLimit = 80;
public float x = 0.0f;
public float y = 0.0f;
public Vector3 distanceVector;
public Camera curCam;
public Camera tarCam;
void CameraSelect() {
//Debug.Log("1");
curCam = Camera.current;
//Debug.Log("2");
if(Input.GetButton("Fire1")){
Debug.Log("3");
RaycastHit rayHit;
Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition);
Debug.Log("4");
if (Physics.Raycast(ray, out rayHit, 1000)){
Debug.Log("5");
curCam.enabled = false;
tarCam = rayHit.transform.gameObject.GetComponentInChildren< Camera >();
tarCam.enabled = true;
}
}
}
// Use this for initialization
void Start () {
x = transform.eulerAngles.y;
y = transform.eulerAngles.x;
distanceVector = new Vector3(0,0,-distance);
if (rigidbody)
rigidbody.freezeRotation = true;
}
// Update is called once per frame
void Update () {
CameraSelect();
if(target){
if(Input.GetMouseButton(1)){
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = (target.transform.position + (rotation * distanceVector));
transform.rotation = rotation;
transform.position = position;
}
}
}
static float ClampAngle (float angle, float min, float max) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
}
GetButtonDown is what you should be doing here. Can you elaborate on why that isn't working for you?
Use GetButtonDown, but put in Debug.Log commands on every second line. Find out what is getting called and what isn't. From what I can see, I see no reason why GetButtonDown isn't working- presumably there's something unusual happening here.
Well now that's just strange. Are you getting to the line before the if statement?
Try using 'Get$$anonymous$$eyDown' ins$$anonymous$$d. Are you getting any errors in the console? Also remember that you will get a lot more 1s and 2s than 3s.
@gilsdank - I fixed up the formatting for you, but in future remember that you can't just copy-paste code directly into questions! The easiest way to fix it up is to select your code segment and press the '101010' button above the dialog box. Also, you can always edit your own posts to repair formatting etc.- you can do more than just apologise!
Alternatively, you can use xml formatting flags 'code' and '/code' at the beginning and end of your code segment (remember that these have to be encased in angle brackets
), or just add four spaces to the beginning of each line.
Answer by clunk47 · Sep 23, 2013 at 05:17 AM
Found a few issues. You were defining a few things within functions instead of at the top of your script, some one of them twice (position), and you were defining curCam but not using it. Try this.
using UnityEngine;
using System.Collections;
public class CameraControl : MonoBehaviour
{
public Transform target;
public Vector3 position;
public float rotateSpeed = .01f;
public float distance = 2.8f;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -80;
public float yMaxLimit = 80;
public float x = 0.0f;
public float y = 0.0f;
public Vector3 distanceVector;
public Camera curCam;
public Camera tarCam;
RaycastHit rayHit;
Ray ray;
Quaternion rotation;
void Update ()
{
CameraSelect();
if(curCam != Camera.current)
curCam = Camera.current;
if(target)
{
if(Input.GetMouseButtonDown(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
rotation = Quaternion.Euler(y, x, 0);
position = (target.transform.position + (rotation * distanceVector));
transform.rotation = rotation;
transform.position = position;
}
}
}
void CameraSelect()
{
if(Input.GetButtonDown("Fire1"))
{
Debug.Log("3");
if(curCam != null)
ray = curCam.ScreenPointToRay(Input.mousePosition);
Debug.Log("4");
if (Physics.Raycast(ray, out rayHit, 1000))
{
Debug.Log("5");
curCam.enabled = false;
tarCam = rayHit.transform.gameObject.GetComponentInChildren< Camera >();
tarCam.enabled = true;
}
}
}
// Use this for initialization
void Start ()
{
x = transform.eulerAngles.y;
y = transform.eulerAngles.x;
distanceVector = new Vector3(0,0,-distance);
if (rigidbody)
rigidbody.freezeRotation = true;
}
// Update is called once per frame
static float ClampAngle (float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
}
this works well with no errors, thank you! however the reason i was using Fire1 ins$$anonymous$$d of get mouse button was because i wanted the controls to work across mulitple platforms, but for now i suppose that wont matter
Just be sure to use GetButtonDown("Fire1") to register a single click, glad I was able to help along with @syclamoth :)
Also keep in $$anonymous$$d you only need your variables to be set public if you either will change values in the inspector, or they will be accessed from other scripts using GetComponent, unless of course they're static, but I'll stop there for now lol.
oh no, you did it with getmousebuttondown... dude you rock! haha thanks again
Be sure to up-vote any answers on UA that help, as well as any comments that helped as well. @syclamoth definitely deserves some karma for all the time he put into this :)