Search Unity

How do I make a player object do an animation when moving

Discussion in 'Scripting' started by RaccoonWithInternetAcces, Jul 13, 2022.

  1. RaccoonWithInternetAcces

    RaccoonWithInternetAcces

    Joined:
    Jul 4, 2022
    Posts:
    11
    Hi, so I followed Brackeys tutorial on making a third person controller using this script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ThirdPersonMovement : MonoBehaviour
    6. {
    7.  
    8.     public CharacterController controller;
    9.     public Transform cam;
    10.  
    11.     public float speed = 6f;
    12.  
    13.     public float turnSmoothTime = 0.1f;
    14.     float turnSmoothVelocity;
    15.  
    16.     void Start()
    17.     {
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         float horizontal = Input.GetAxisRaw("Horizontal");
    24.         float vertical = Input.GetAxisRaw("Vertical");
    25.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    26.  
    27.         if (direction.magnitude >= 0.1f)
    28.         {
    29.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    30.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    31.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    32.  
    33.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    34.             controller.Move(moveDir.normalized * speed * Time.deltaTime);
    35.         }
    36.  
    37.     }
    38. }
    Now, I have a cube as a player object, I quickly made an animation with the animation tab, but now how do i make the cube do the animation when moving? Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Go find a tutorial for moving and animating. NOBODY is going to retype a tutorial here in this little tiny text box.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  3. RaccoonWithInternetAcces

    RaccoonWithInternetAcces

    Joined:
    Jul 4, 2022
    Posts:
    11
    Thanks but, i posted here because i couldn't find any tutorial...
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    How to create an animator controller:


    How to create a script to control your animator controller state:


    Edit: or you can skip all of the above and use animation.Play. It's easier to start-off with if you are only doing a small number of animations:
    https://docs.unity3d.com/ScriptReference/Animation.Play.html
     
    Last edited: Jul 14, 2022
  5. RaccoonWithInternetAcces

    RaccoonWithInternetAcces

    Joined:
    Jul 4, 2022
    Posts:
    11
    Thanks! I think I found a script that could work, now, since the object that i need to animate is inside the game object with the script, how can I make the script edit the properties of the object inside the game object?

    Code (CSharp):
    1.          if (horizontal != 0 || vertical != 0)
    2.         {
    3.             anim.SetBool("Walk", true);
    4.         }
    5.         else
    6.         {
    7.             anim.SetBool("Walk", false);
    8.         }
     
  6. RaccoonWithInternetAcces

    RaccoonWithInternetAcces

    Joined:
    Jul 4, 2022
    Posts:
    11
    Thanks! I think I found a script that could work, now, since the object that i need to animate is inside the game object with the script, how can I make the script edit the properties of the object inside the game object?

    Code (CSharp):
    1.          if (horizontal != 0 || vertical != 0)
    2.         {
    3.             anim.SetBool("Walk", true);
    4.         }
    5.         else
    6.         {
    7.             anim.SetBool("Walk", false);
    8.         }