Search Unity

Mecanim Character Pivot/Rotate

Discussion in 'Scripting' started by mariaxx77, Mar 14, 2014.

  1. mariaxx77

    mariaxx77

    Joined:
    Nov 17, 2013
    Posts:
    42
    Hi Everyone,

    I am developing my first game. I have a character i have made with Mecanim to control its movements, (using the 45min mecanim video given by Unity)

    Currently my character can stay idle, walk forward with left and right motion and run forward with left and right motion as well as go backwards.

    However i also want to allow my character to simple pivot in the idle position around the game world (without moving forward or backwards) so when i press the left key it simple rotates the player left around the gameworld (along with the camera) and if the user presses the right key then the character pivots right (along with the camera). Im not sure if this animation or a scripting problem


    I hope someone can help!!
    Thank you
    I have also included the control script if that is needed :)

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. // Require these components when using this script
    4. [RequireComponent(typeof (Animator))]
    5. [RequireComponent(typeof (CapsuleCollider))]
    6. [RequireComponent(typeof (Rigidbody))]
    7. public class GirlControlScript : MonoBehaviour
    8. {
    9.     [System.NonSerialized]                 
    10.     public float lookWeight;                    // the amount to transition when using head look
    11.    
    12.     [System.NonSerialized]
    13.     public Transform enemy;                     // a transform to Lerp the camera to during head look
    14.    
    15.     public float animSpeed = 1.5f;              // a public setting for overall animator animation speed
    16.     public float lookSmoother = 3f;             // a smoothing setting for camera motion
    17.     public bool useCurves;                      // a setting for teaching purposes to show use of curves
    18.  
    19.    
    20.     private Animator anim;                          // a reference to the animator on the character
    21.     private AnimatorStateInfo currentBaseState;         // a reference to the current state of the animator, used for base layer
    22.     private AnimatorStateInfo layer2CurrentState;   // a reference to the current state of the animator, used for layer 2
    23.     private CapsuleCollider col;                    // a reference to the capsule collider of the character
    24.    
    25.  
    26.     static int idleState = Animator.StringToHash("Base Layer.Idle");            // these integers are references to our animator's states  
    27.     static int locoState = Animator.StringToHash("Base Layer.Locomotion");      // and are used to check state for various actions to occur
    28.            
    29.     public bool IsAlive = true;
    30.  
    31.     void Start ()
    32.     {
    33.         // initialising reference variables
    34.         anim = GetComponent<Animator>();                     
    35.         col = GetComponent<CapsuleCollider>();             
    36.         if(anim.layerCount ==2)
    37.             anim.SetLayerWeight(1, 1);
    38.     }
    39.    
    40.    
    41.     void FixedUpdate ()
    42.     {
    43.         if(IsAlive)
    44.         {
    45.        
    46.             //Debug.Log("hi");
    47.             float h = Input.GetAxis("Horizontal");              // setup h variable as our horizontal input axis
    48.         float v = Input.GetAxis("Vertical");                // setup v variables as our vertical input axis
    49.         anim.SetFloat("Speed", v);                          // set our animator's float parameter 'Speed' equal to the vertical input axis             
    50.         anim.SetFloat("Direction", h);                      // set our animator's float parameter 'Direction' equal to the horizontal input axis       
    51.         anim.speed = animSpeed;                             // set the speed of our animator to the public variable 'animSpeed'
    52.         anim.SetLookAtWeight(lookWeight);                   // set the Look At Weight - amount to use look at IK vs using the head's animation
    53.         currentBaseState = anim.GetCurrentAnimatorStateInfo(0); // set our currentState variable to the current state of the Base Layer (0) of animation
    54.                 RaycastHit hit;
    55.         if (Physics.Raycast(transform.position, Vector3.forward, out hit))
    56.             {
    57.             v = 0; 
    58.             }
    59.             //Debug.DrawRay(transform.position, fwd);
    60.         if(anim.layerCount ==2)    
    61.             layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1);   // set our layer2CurrentState variable to the current state of the second Layer (1) of animation
    62.         }
    63.        
    64.        
    65.         else
    66.         {
    67.                
    68.         }
    69.        
    70.        
    71.  
    72.        
    73.     }
    74. }
    75.  
     
  2. heyokwhy

    heyokwhy

    Joined:
    Aug 5, 2014
    Posts:
    2
    Hi, did you solve the problem ?
    I'd follow the same tutorial as you, and have the same issue that you had.
    I want to pivot my character, even when he is in his idle animation.
    I've made a nice blend tree, following this video :


    So i've made the blend trees, it works fine in the previews, but then, the scripts that the guy give in his video to tell to your character to pivot dosent work, and i dont know what can i add in my script ( bot control script ) that i already have, witch was in the tutorial you saw too, to make my character play the pivot blend trees that i've made.

    Best
     
    webwizo likes this.
  3. igfados

    igfados

    Joined:
    Mar 23, 2015
    Posts:
    1
    Do you find the solution yet? i use unity 5 and all that guy script have was messed up in unity 5, it say all compiler error and have to be fixed.
     
  4. ZenMicro

    ZenMicro

    Joined:
    Aug 9, 2015
    Posts:
    206
    You asked for simple so here it is. You can simply rotate the character when idle based on mouse rotation (or keys if you wish) transform in this case is the player GameObject (and the character model) This is called in FixedUpdate for me. I hope this is what you meant;

    Code (CSharp):
    1. float rotY = Input.GetAxis("Mouse X");  // Get the Yaw Input
    2. if (currentBaseState.fullPathHash == idleState){
    3. transform.Rotate(0, rotY, 0);} // Apply Mouse rotation to model if idle (can look around)
    4.