Hello. I’m trying to create a split arrow spell with code by spawning, adding spacing, and rotations to the arrows. The arrows spawn and move forward, but there are two issues I can’t figure out:
- Only the first arrow is rotating.
- The arrows aren’t moving in the direction they were rotated to.
Here is my code (See screenshot for output):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SplitArrow : MonoBehaviour
{
[SerializeField] private GameObject arrow;
[SerializeField] private Transform spawnPoint;
[SerializeField] private float projectileSpeed = 10.0f;
[SerializeField] private float xSpacing = 2.0f;
[SerializeField] private float yRot = 35.0f;
public List<GameObject> arrowList = new List<GameObject>();
private void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha2))
{
for (int i = 0; i < arrowList.Count; i++)
{
GameObject projectile = Instantiate(
arrowList[i],
new Vector3(spawnPoint.position.x + xSpacing * i, spawnPoint.position.y, spawnPoint.position.z + 5.0f),
new Quaternion(arrowList[i].transform.rotation.x, arrowList[i].transform.rotation.y + (yRot * i), arrowList[i].transform.rotation.z, arrowList[i].transform.rotation.w)
);
projectile.GetComponent<Rigidbody>().velocity = arrowList[i].transform.forward * projectileSpeed;
Destroy(projectile, 2f);
}
}
}
}