Search Unity

Change an objects animation depending on it "speed"?

Discussion in 'Animation' started by Dude_Mojo, Jun 14, 2017.

  1. Dude_Mojo

    Dude_Mojo

    Joined:
    Jul 14, 2016
    Posts:
    13
    How would one go about changing an objects animation depending on the speed that the object is moving. I am trying to make a point and click adventure in unity 3D and im having trouble with my "Speed" parameter. This is the code that I have applied to my player object that has the animator component attached to it. But for one reason or another I cant get it to work properly. Im new to unity and Im not very sure what im doing. here is my code for identifying the objects speed "I think". Any help is appreciated ;D

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HowFastMove : MonoBehaviour
    6. {
    7.     Animator animator;
    8.  
    9.     void Start()
    10.     {
    11.  
    12.    
    13.         animator = GetComponent<Animator>();
    14.  
    15.     }
    16.     void Update()
    17.     {
    18.        
    19.         float move = Input.GetAxis("Horizontal");
    20.        
    21.         animator.SetFloat("speed", move);
    22.  
    23.     }
    24. }
    This is my mouse click to move script. Maybe its better to combine them?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ClickToMove : MonoBehaviour
    5. {
    6.     UnityEngine.AI.NavMeshAgent navAgent;
    7.  
    8.    
    9.     void Start()
    10.     {
    11.         navAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
    12.     }
    13.  
    14.    
    15.     void Update()
    16.     {
    17.         RaycastHit hit;
    18.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    19.  
    20.         if (Input.GetMouseButtonDown(0)) // Remember 0 is Left-Click, 1 is Right-Click
    21.         {
    22.             if (Physics.Raycast(ray, out hit, 100))
    23.             {
    24.                 navAgent.SetDestination(hit.point);
    25.             }
    26.         }
    27.     }
    28. }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,442