I don't know anything about Quaternions

I have a ConfigurableJoint body that points at an object but I want to use its orientation then make it point towards all other dinos. here’s the code:

    private void Update()
    {
        TurnTowards();
        if (isWalking)
        {
            leftLeg.UpdateLeg(-walkcycle);
            rightLeg.UpdateLeg(walkcycle);
            walkcycle += walkSpeed * Time.deltaTime;
        }
        else
        {
            rightLeg.UpdateLegStationary();
            leftLeg.UpdateLegStationary();
        }
        Boid(dinos);
    }

    private void Boid(string dinos)
    {
        Quaternion startDir = body.targetRotation;
        Quaternion target = startDir;
        foreach (GameObject dino in GameObject.FindGameObjectsWithTag(dinos))
        {
            if (!dino.transform.IsChildOf(transform) && Vector3.Distance(dino.transform.position, body.transform.position) < maxHerdDist)
            {
                Vector3 direction = dino.transform.position - body.transform.position;
                direction = Vector3.ProjectOnPlane(direction, Vector3.up);
                target *= Quaternion.LookRotation(direction);
                //target.y += Quaternion.Inverse(Quaternion.LookRotation(direction)).y / join * Vector3.Distance(dino.transform.position, body.transform.position);
            }
        }

        body.targetRotation = target;
    }

    void TurnTowards()
    {
        Vector3 direction = player.position - body.transform.position;
        direction = Vector3.ProjectOnPlane(direction, Vector3.up);
        body.targetRotation = Quaternion.Inverse(Quaternion.LookRotation(direction));
    }

The problem with the result is the joint is constantly spinning in circles.

All about Euler angles and rotations, by StarManta:

https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

For articulated stuff the best solution will (almost always) be some kind of bone-like hierarchy with each bone controlling only a specific local axis of rotation…

Also, this snippet (commented out above):

That is NOT a degree. That is NOT an angle. Do not mess with .x, .y, .z and .w of a Quaternion.

1 Like