Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Why can't I get ball movement with this script?

$
0
0
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

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>