- Home /
Calling Update() from Pointer Down not working properly
I have one script that I intend to use to apply a force to a game object (lander) in the direction it is facing. I only want to apply this force when a UI Button is depressed however.
When I attach the script directly to the button the lander moves as intended, fluidly and briskly, but obviously it moves independently of whether the button is pressed or not. When I call the Update function in a pointer down event trigger however, it barely moves at all when I depress the button - a pixel or so a frame it looks like. Code is below:
Also, I read the docs and it said not to put GameObject.Find in Update() but this wouldn't work otherwise I think. That's why its commented out in Start() and put in Update(). I'm really new to this so if I'm being obviously stupid, call me out on it.
Cheers
using UnityEngine;
using System.Collections;
public class thrust : MonoBehaviour {
private GameObject lander;
// Use this for initialization
public void Start () {
//lander = GameObject.Find ("Lander");
}
// Update is called once per frame
public void Update () {
lander = GameObject.Find ("Lander");
float thrustval = 5;
float angle = lander.transform.eulerAngles.z;
//print (angle);
float ycomp = thrustval * Mathf.Cos (angle * Mathf.Deg2Rad);
float xcomp = -1 * (thrustval * Mathf.Sin (angle * Mathf.Deg2Rad));
Vector2 components = new Vector2 (xcomp,ycomp);
lander.rigidbody2D.AddForce (components,ForceMode2D.Force);
}
}