Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help with animations

Discussion in 'Animation' started by FunnyProducts, May 3, 2022.

  1. FunnyProducts

    FunnyProducts

    Joined:
    Apr 16, 2019
    Posts:
    1
    my third person game movement worked like a charm before importing a new map. Now when it animates the player moves towards the initial animation recording coordinate


    i've tried setting an empty gameobject as the parent -> didnt work
    i've tried setting animations as root motion ->it worked, but it wouldnt let my character jump or dash properly

    here is my movement script :
    public class PlayerController : MonoBehaviour
    {
    //public variables
    public Animator animator;
    public Camera mainCamera;
    public GetInside firepit_prefab;
    public float moveSpeed;
    public float jumpSpeed;
    public float gravity;
    public float jumpCount;
    private float MaxjumpCount;
    //private variables
    public CharacterController _controller;
    private float _directionY;
    public bool canLook;

    void Start()
    {
    //firepit_prefab = GetComponent<GetInside>();
    canLook = true;

    MaxjumpCount = jumpCount;
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.E))
    {
    if(firepit_prefab.inside == true)
    { //enter battle script here
    Debug.Log("interacted = yes");
    firepit_prefab.Battle();
    }
    }
    //check for walking, apply walking animation
    if (Input.GetKey(KeyCode.S)) {
    animator.Play("walking");
    }
    if (Input.GetKey(KeyCode.W))
    {
    animator.Play("walking");
    }
    if (Input.GetKey(KeyCode.A))
    {
    animator.Play("walking");
    }
    if (Input.GetKey(KeyCode.D))
    {
    animator.Play("walking");
    }

    //script for player to look at mouse
    Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
    Plane groundPlane = new Plane(Vector3.up, new Vector3(0, this.transform.position.y, 0));
    float rayLength;

    if (groundPlane.Raycast(cameraRay, out rayLength))
    {
    Vector3 pointToLook = cameraRay.GetPoint(rayLength);
    Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
    if (canLook == true)
    {

    transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));



    }
    }



    //moving
    float horizontalInput = Input.GetAxis("Horizontal");
    float verticalInput = Input.GetAxis("Vertical");
    Vector3 direction = new Vector3(horizontalInput,0,verticalInput);
    if (_controller.isGrounded)
    {
    jumpCount = MaxjumpCount;

    }
    if (Input.GetButtonDown("Jump")&& _controller.isGrounded)
    {
    jumpCount-=1;

    _directionY = jumpSpeed;
    // animator.Play("jump");
    }
    if (Input.GetButtonDown("Jump") && !_controller.isGrounded && jumpCount >0)
    {
    _directionY = jumpSpeed;
    jumpCount-=1;

    //animator.Play("jump");
    }
    _directionY -= gravity * Time.deltaTime;
    direction.y = _directionY;
    _controller.Move(direction*moveSpeed*Time.deltaTime);
    }


    }