using System.Collections; using System.Collections.Generic; using UnityEngine; public…
- User
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BowAttack : MonoBehaviour { public float arrowSpeed = 1; public GameObject arrowPrefab;
codeRigidbody2D rigid; private float fireDelay = 1f; // Delay time in seconds private float lastFireTime; void Start() { rigid = GetComponent<Rigidbody2D>(); } void Update() { // Check if enough time has passed since the last firing if (Time.time - lastFireTime >= fireDelay) { //Fire when we click the mouse if (Input.GetMouseButtonDown(0)) { //Get the vector from the player to the mouse Vector3 mousePos = Input.mousePosition; mousePos.z = 10; Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos); Vector3 p2m = worldPos - transform.position; p2m.Normalize(); //Spawn an arrow GameObject go = Instantiate<GameObject>(arrowPrefab); go.transform.position = transform.position; go.transform.right = p2m - go.transform.position; //Shoot it in that direction Rigidbody2D arrowRigid = go.GetComponent<Rigidbody2D>(); arrowRigid.velocity = p2m * arrowSpeed; // Update the last firing time lastFireTime = Time.time; } } }}
make it so the arrow is being instantiated as a child of the player
- Assistant
To instantiate the arrow as a child of the player, you can modify the code in the
Update()function as follows:c# - User
make it so the arrows velocity is normalized
- Assistant
To normalize the arrow's velocity, you can modify the code in the
Update()function as follows:c#