// Update is called once per frame void Update() { if (Input.GetMouseButton(1) && canshoot) //Right Mouse Button to shoot { if (ammo > 0) { GameObject Iarrow; Vector3 mouse = Input.mousePosition; mouse.z = 10; mouse = Camera.main.ScreenToWorldPoint(mouse); Vector3 player = transform.position; float x = mouse.x - player.x; float y = mouse.y - player.y; Vector3 vel = new Vector3(x, y, 0); vel = arrowSpeed * vel.normalized; float angle = Mathf.Atan2(y, x) * Mathf.Rad2Deg; Iarrow = Instantiate(arrow, transform.position + vel.normalized * 0.25f, new Quaternion(0, 0, 0, 0)); rb = Iarrow.GetComponent <Rigidbody2D> (); rb.velocity = vel; Iarrow.transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward); Iarrow.GetComponent <arrow>().ttag = this.gameObject.tag; StartCoroutine(cooldown(time)); ammo = ammo - 1; } } }
void FixedUpdate() { if (canshoot && range) //Makes an arrow, launches arrow at player's current location, can optimize with multiplication { GameObject Iarrow; //movespeed = Player.GetComponent<Rigidbody2D>().velocity; Vector3 toplayer = Player.transform.position - transform.position; float angle = Mathf.Atan2(toplayer.y, toplayer.x) * Mathf.Rad2Deg; Iarrow = Instantiate(arrow, transform.position + toplayer.normalized * 0.25f, new Quaternion(0, 0, 0, 0)); rb = Iarrow.GetComponent <Rigidbody2D>(); Iarrow.GetComponent <arrow>().ttag = this.gameObject.tag; Iarrow.transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward); //ME FIDDLING AROUND WITH PREDICTIVE SHOOTING, CAN'T QUITE FIGURE IT OUT //Vector3 predict = ((transform.position.x - (arrowSpeed/movespeed.x)*Player.transform.position.x)/(1-(arrowSpeed/movespeed.x)), (transform.position.y - (arrowSpeed/movespeed.y)*Player.transform.position.y)/(1-(arrowSpeed/movespeed.y)), 0); ).normalized //Vector3 predict = (transform.position - (arrowSpeed/movespeed.magnitude)) / (1 - (arrowSpeed/movespeed.magnitude)); //Vector3 predict = - ((arrowSpeed / (arrowSpeed - movespeed.magnitude)) * toplayer).normalized; //Vector3 newtoplayer = toplayer + movespeed rb.velocity = toplayer.normalized * arrowSpeed; StartCoroutine(cooldown(time)); } }