Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to limit the movement to only move the z axis

How to limit my ai movement to move only on the z axis. Ive already tried freezing the rotation and position on it’s rigidbody but instead when i jump my ai also goes up with me on it’s position and slightly rotates towards me.

private void Update()
    {
        StopFollowing();

        Vector3 relativePos = target.position - transform.position;
        Quaternion rotation = Quaternion.LookRotation(relativePos);

        Quaternion current = transform.localRotation;

        transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime
            * LookSpeed);



    }
    // Update is called once per frame
    void followPlayer()
    {
        Vector3 pos = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
        rig.MovePosition(pos);
        
;
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Freezing the rigidbody will only affect physics (using AddForce or rb.velocity)

You are using MoveTowards (basically teleporting it ignoring physics)

But you can limit it to Z-Axis manually:

void followPlayer()
{
    Vector3 targetPos = target.position; // copy target Position
    targetPos.x = transform.position.x; // keep x
    targetPos.y = transform.position.y; // keep y
    Vector3 pos = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime); // now only considers z-difference
    rig.MovePosition(pos);
}

You basically modify the target Position to be equal on the x and y axis, so that the MoveTowards only uses Z effectively.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading