#Player glitches out when flying

1 messages · Page 1 of 1 (latest)

steep girder
#

I have made a separate flying script, a button script and a separate set of animation, and for some reason the player is just stuck and when he flies he goes somewhere random and glitches back
(sorry for bad recording quality my disk is being bombarded rn)

#

here is the PressFlyButton.cs script

using Unity.VisualScripting;
using UnityEditor.Animations;
using UnityEngine;

public class PressFlyButton : MonoBehaviour
{
    public Transform player;
    public Animator animator;
    public Renderer renderer;
    public PlayerMovement playerMovement;
    public NewBehaviourScript behaviourScript;
    public RuntimeAnimatorController controller;
    // Update is called once per frame
    void Update()
    {
        if ((Input.GetKeyDown("e")) && (player.position.x > 73) && (player.position.x < 74.5))
        {
            pressbutton();
        }
    }
    void pressbutton()
    {
        animator.runtimeAnimatorController = controller;
        renderer.enabled = true;
        playerMovement.enabled = false;
        behaviourScript.enabled = true;

    }
}
#

and here is the PlayerFly.cs script

using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
    public Vector3 MovementForce;
    public Vector3 UpForce;
    public Rigidbody2D rb;
    public Animator animator;
    bool moveright = false;
    bool moveleft = false;
    bool jump = false;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("d"))
        {
            moveright = true;
            animator.SetBool("FlyRight", true);
        }   

        if (Input.GetKey("a"))
        {
            moveleft = true;
            animator.SetBool("FlyLeft", true);
        }

        if (Input.GetKeyDown("space")) 
        { 
            jump = true;
        }
    }
    void FixedUpdate()
    {
        if (moveright == true)
        {
            transform.position += MovementForce;
            moveright = false;
            animator.SetBool("FlyRight", false);
        }
        if (moveleft == true)
        {
            transform.position -= MovementForce;
            moveleft = false;
            animator.SetBool("FlyLeft", false);
        }
        if (jump == true)
        {
            rb.AddForce(UpForce);
            jump = false;
        }
    }
}
#

i am also getting this error but i do not understand what it means, all of the variables have been assigned a value

UnassignedReferenceException: The variable animator of PressFlyButton has not been assigned.
You probably need to assign the animator variable of the PressFlyButton script in the inspector.
PressFlyButton.pressbutton () (at Assets/Scripts/PressFlyButton.cs:23)
PressFlyButton.Update () (at Assets/Scripts/PressFlyButton.cs:18)
teal aurora
#

this is a dots forum and it doesn't look like your using dots, the error is most likely due to not being assigned in the inspector (click game object in hierarchy, assign the animator to the animator field) for better help you should post in code general or other related forums, good luck!

steep girder
#

[]close