Home > front end >  SOLVED - Unity C# - Rotating more than one object at a time with quaternions isn't working
SOLVED - Unity C# - Rotating more than one object at a time with quaternions isn't working

Time:07-19

I have several cannon objects that need to rotate to point at their targets and fire a cannon ball. As long as only one cannon object is in the scene it works just fine. If I add any additional canons they only aim on the y axis (turning left and right), but they don't rotate to point in the random angle assigned on the x axis (aiming up and down). I don't understand at all why this would happen. Please help? :)

A script is attached to each cannon with that has this piece of code in it. I don't see why it should interfere with other cannon scripts...?

private IEnumerator Aim(float durationToRot)
{
    if (rotating)
    {
        yield break;
    }
    rotating = true;

    target = GetLandPoint();
    angle = Random.Range(25, 45);
    Quaternion desiredAngle = Quaternion.Euler(angle, 0, 0);
    Quaternion lookRot = Quaternion.LookRotation(target - transform.position);
    Quaternion lookAngle = Quaternion.Euler(desiredAngle.eulerAngles.x, lookRot.eulerAngles.y, 0);

    turnTime = 0f;
    while (turnTime < durationToRot)
    {
        transform.rotation = Quaternion.Slerp(transform.rotation, lookAngle, turnTime / durationToRot);
        turnTime  = Time.deltaTime;

        yield return null;
    }
    transform.rotation = lookAngle;
    rotating = false;
    FireCannon(target);
}

CodePudding user response:

I tried your script and everything is working fine, maybe you made mistake somewhere else, is your cannons child of another cannons, if so, then try to use localRotation instead of rotation.

CodePudding user response:

Thank you both for answering, but I found why I was having trouble. A part of my cannon prefab was toggled to "static" somehow... omg.. Still not sure why a single instance of the cannon would work though when multiples of the same object didn't work.. odd.

  • Related