简体   繁体   中英

Stuck in 3rd person player animation / Unity

So i was trying 3rd character movement in unity an got stuck in here. The player animation is moving in loop but player is moving ahead of camera and than coming back, its doing it every cycle.

Let me provide char_control code and video link.

video link - https://drive.google.com/file/d/15VEIcOqy7yhQfACT4Pjxh-BZoVsrYdFS/view?usp=sharing


code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class char_control : MonoBehaviour
{

    public GameObject Player;
    //variable - type of variable

    public bool isRunning;
    public float horizontal_move;
    public float vertical_move;

    // Update is called once per frame
    void Update()
    {
        
        if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
        {
            Player.GetComponent<Animation>().Play("Running");
            horizontal_move = Input.GetAxis("Horizontal") * Time.deltaTime * 100;
            vertical_move = Input.GetAxis("Vertical") * Time.deltaTime * 1;
            isRunning = true;
            transform.Rotate(0, horizontal_move, 0);
            transform.Translate(0, 0, vertical_move);
        }
        else
        {
            Player.GetComponent<Animation>().Play("Idle");
            isRunning = false;
        }

    }
}

Unity controls character movement and movement animation:

[System.Serializable]
public class Anim//Game control animation
{
    public AnimationClip idle;
    public AnimationClip runForward;
    public AnimationClip runBackward;
    public AnimationClip runRight;
    public AnimationClip runleft;
}


public class playermove : MonoBehaviour {
public float h = 0.0f;
public float v = 0.0f;
//assign the variable,
private Transform tr;
//move speed variable
public float movespeed = 10.0f;
//Rotate can use the Rotate function,
public float rotSpeed ​​= 100.0f;
//The animation class variable to display to the inspector
public Anim anim;
//To access the variables of the following 3d model animation component objects
public Animation _animation;

// Use this for initialization
void Start () {
    //Assign the Tr component to the initial part of the script
    tr = GetComponent<Transform>();
    
    // Find the anim component that is subordinate to itself and assign it to a variable.
    _animation = GetComponentInChildren<Animation>();


    _animation.clip = anim.idle;
    _animation.Play();

}

// Update is called once per frame
 void Update() {
     h = Input.GetAxis("Horizontal");
     v = Input.GetAxis("Vertical");


    Debug.Log("H=" + h.ToString());
    Debug.Log("V="+ v.ToString());


    //Calculate the moving direction vector of left, right, front and rear.
    Vector3 moveDir = (Vector3.forward * v) + (Vector3.right * h);


    //translate (movement direction *time.deltatime*movespeed, space.self)
    tr.Translate(moveDir.normalized *Time.deltaTime*movespeed , Space.Self);


    //vector3.up axis as the benchmark, rotate at rotspeed speed
    tr.Rotate(Vector3.up * Time.deltaTime * rotSpeed ​​* Input.GetAxis("Mouse X"));


    if (v >= 0.1f)
    {
        //forward animation
        _animation.CrossFade(anim.runForward.name, 0.3f);
    }
    else if (v <= -0.1f)
    {
        //back animation
        _animation.CrossFade(anim.runBackward.name, 0.3f);
    }
    else if (h >= 0.1f)
    {
        //right animation
        _animation.CrossFade(anim.runRight.name, 0.3f);
    }
    else if (h <= -0.1f)
    {
        //left animation
        _animation.CrossFade(anim.runleft.name, 0.3f);
    }
    else
    {
        _animation.CrossFade(anim.idle.name, 0.3f);
     }

      //Based on the keyboard input value, execute the animation to be operated     
   }
}

hope it helps you.

You should use Animator and Animator Controller for your purpose. To fix your problem you could probably disable root motion in your animation (Bake Into Pose on Root Transform Position (XZ), setting up Mask should work too) but still you will end up using Animator Controller so better do that asap

Have you checked that your "Running" animation has both "Loop Time" and "Loop Pose" checked. I know that the pose option is the one which makes my character act the way you described.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM