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

How to make arrows fly to user mouse position?

$
0
0
I'm trying to make 2d platformer with bow shooting, but I can't understand how to make arrows fly to user mouse position. With my code arrows randomly fly either to correct point or to right from player. if (Input.GetMouseButtonDown(0)) { // set animation trigger(animation calls createArrow method) keyDown = true; animator.SetTrigger("StartShoot"); } if (Input.GetMouseButtonUp(0) && arrow) { animator.ResetTrigger("StartShoot"); ShootArrow(); keyDown = false; } // while mouse key pressed arrow rotates after mouse position if (keyDown && arrow) { Vector3 bowPosition = transform.position; Vector3 mousePosition = Input.mousePosition; mousePosition.z = Camera.Instance.Distance; Vector3 arrowDirection = UnityEngine.Camera.main.ScreenToWorldPoint(mousePosition) - bowPosition; arrow.transform.right = arrowDirection; } void CreateArrow() { arrow = Instantiate(arrowPrefab, arrowSpawn.position, Quaternion.identity); arrow.GetComponent().connectedBody = rigidboby; } void ShootArrow() { arrow.GetComponent().enabled = false; arrow.GetComponent().AddForce(arrow.transform.right * arrowForce,ForceMode2D.Impulse); arrow = null; }

Viewing all articles
Browse latest Browse all 1264

Trending Articles



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