I have been trying to figure out how to add an impulse force on the ball with a button press. I can get movement from the ball with the commented out OnMouseDown() method, but I cannot figure out how to do it otherwise. Here is the code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.Events;
using System.Timers;
public class LaunchBall : MonoBehaviour
{
public Rigidbody rb;
public float forceOnBall;
public float percentage = 0;
public bool buttonPressed = false;
public int sign = 1;
/* void OnMouseDown()
{
GetComponent().AddForce(transform.right * 500);
GetComponent().useGravity = true;
} */
void Start()
{
rb = GetComponent();
}
public void ButtonDown()
{
buttonPressed = true;
}
public void ButtonUp()
{
buttonPressed = false;
}
public void Update()
{
if (buttonPressed)
{
percentage+=sign;
Debug.Log(percentage);
if (percentage == 100 || percentage == 0)
sign *= -1;
}
}
public void HitBall()
{
forceOnBall = percentage * 100;
if (!buttonPressed && percentage > 0)
{
rb.AddForce(0, 0, forceOnBall, ForceMode.Impulse);
}
}
}
![alt text][1]
[1]: /storage/temp/81745-unity-screenshot.png
↧