Hi all. I’m having issues with particle rotation. Basically, when I created the particle the front was facing the x axis (its side is facing z). I can rotate it 90 in y to have it facing in the correct direction, but I’m trying to instantiate it at the same position and rotation as another object (the firePoint) and use transform.forward to move the particle after it’s instantiated. The particle’s transform.forward is its side, though.
I have no idea whether I’m explaining this correctly. Essentially, when I instantiate the particle at the firePoint, its side is facing the z axis and I don’t know how to make it face the correct direction.
Here are the scripts (ignore the badly named classes):
public class RotateCube : MonoBehaviour {
public Camera cameraMain;
public float maximumLength;
Ray rayMouse;
Vector3 pos;
Vector3 dir;
Quaternion rotation;
private void Update()
{
RaycastHit hit;
Vector3 mousePos = Input.mousePosition;
rayMouse = cameraMain.ScreenPointToRay(mousePos);
if(Physics.Raycast(rayMouse.origin, rayMouse.direction, out hit, maximumLength))
{
RotateToMouse(gameObject, hit.point);
}
else
{
Vector3 pos = rayMouse.GetPoint(maximumLength);
RotateToMouse(gameObject, pos);
}
}
void RotateToMouse(GameObject ourObject, Vector3 destination)
{
dir = destination - ourObject.transform.position;
rotation = Quaternion.LookRotation(dir);
ourObject.transform.localRotation = Quaternion.Lerp(ourObject.transform.rotation, rotation, 1);
}
public Quaternion GetRotation()
{
return rotation;
}
}
public class MoveProjectile : MonoBehaviour
{
public float speed;
Fire fire = new Fire();
private void Update()
{
transform.position += transform.forward * speed * Time.deltaTime;
}
}
public class Fire : MonoBehaviour {
public GameObject prefab;
public RotateCube rotateCube;
public Transform firePoint;
private void LateUpdate()
{
SpawnProjectile();
}
private void SpawnProjectile()
{
if (Input.GetMouseButtonDown(0))
{
GameObject projectile = Instantiate(prefab, firePoint.position, Quaternion.identity );
if (rotateCube != null)
{
projectile.transform.localRotation = rotateCube.GetRotation();
}
}
}
}
Can anyone help me? I’ve been trying to figure this out for hours and I’m at a loss.