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

Shooting a bullet that stops in place when mouse is released

$
0
0
Ive tried this but it doesn't work because I can't reference the instance and it's properties in in else{ or anywhere outside the if statement. How would I go about making it work? using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewBehaviourScript : MonoBehaviour { public Transform firePoint; public GameObject ballPrefab; public float balls; public float maxBalls; public float ballForce = 13f; private bool spawn = true; void Update() { if (Input.GetMouseButton(0) && balls < maxBalls) { if (spawn == true) { GameObject ball = Instantiate(ballPrefab, firePoint.position, firePoint.rotation); Rigidbody2D rb = ball.GetComponent(); rb.AddForce(firePoint.up * ballForce, ForceMode2D.Impulse); balls++; spawn = false; } } else { rb.velocity = Vector3.zero; rb.angularVelocity = 0f; rb.Sleep(); } } } newb btw

Viewing all articles
Browse latest Browse all 1264

Trending Articles



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