Jump to content
  • Advertisement
corsair990

Player Gameplay Mechanics

Recommended Posts

Hello, all. I am a unity 5 user and I can not figure out how to correct my problem. I have a blend tree called blendSpeed that I am trying to control through animator by getting the component only when I call velocity. My character freaks out and I can not figure it out I have tried multiple ways for the past two days passining various vector3 values and floats setting  velocity and calling magnitude in the fixed update. I believe my problem is my use of transform.Translate to move my characters horizontal and vertical. I Think I am not understanding the mathematics involved so if anyone can point me in the right direction to correct my problem, it would be greatly appreciative.

 

 

public class PlayerInput : MonoBehaviour
{
    public float movementSpeed = 10;
    public float turningSpeed = 60;

    private Rigidbody playerRigidBody;
    private Animator anim;

    private float horizontal;
    private float vertical;
    private Vector3 currentVelocity;

    private Vector3 previousPosition;
    private Vector3 currentPosition;
    private Vector3 moveVelocity;
    private Vector3 moveInput;

    void Start()
    {
        playerRigidBody = GetComponent<Rigidbody>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update ()
    {
        float mouseX = Input.GetAxis("Mouse X") * turningSpeed * Time.deltaTime;
        transform.Rotate(0, mouseX, 0);

        vertical = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
        transform.Translate(0f, 0f, vertical);

        horizontal = Input.GetAxis("Horizontal") * movementSpeed * Time.deltaTime;
        transform.Translate(horizontal, 0f, 0f);


        Animating();

    }

    void FixedUpdate()
    {
        if (vertical > 0 || horizontal > 0)
        {
        playerRigidBody.velocity = (transform.position);
        }
    }

    void Animating()
    {
        anim.SetFloat("blendSpeed", playerRigidBody.velocity.magnitude);
    }

}

 

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!