Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Harardin · Aug 10, 2015 at 08:09 AM · animationanimatorgravityscriptingbasicsscriptingproblem

Issue with animation and gravity.

Hi to everyone, I have a small problem when my char idle it fly above the ground. Character snaps to the ground only when it go strait, with his anim. But when move another direction with other animation it go above the ground, The other animations I used from Mecanim Animation Tutorial https://www.youtube.com/watch?v=Xx21y9eJq1U&feature=kp≈p=desktop Only strait move animation create my self. To play this animation and move I use two scripts. The first one is rigBodyControl from Unity Wiki: http://wiki.unity3d.com/index.php/RigidbodyFPSWalker using UnityEngine; using System.Collections;

[RequireComponent(typeof(Rigidbody))] [RequireComponent(typeof(CapsuleCollider))] public class rigBodyControl : MonoBehaviour { public float speed = 10.0f; public float gravity = 10.0f; public float maxVelocityChange = 10.0f; public bool canJump = true; public float jumpHeight = 2.0f; private bool grounded = false;

 void Awake()
 {
     rigidbody.freezeRotation = true;
     rigidbody.useGravity = false;
     
 }
 
 void FixedUpdate()
 {
     GetComponent<BotControlScript>();
     if (grounded)
     {
         // Calculate how fast we should be moving
         Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         targetVelocity = transform.TransformDirection(targetVelocity);
         targetVelocity *= speed;
         
         // Apply a force that attempts to reach our target velocity
         Vector3 velocity = rigidbody.velocity;
         Vector3 velocityChange = (targetVelocity - velocity);
         velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
         velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
         velocityChange.y = 0;
         rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
         
        
         // Jump
         if (canJump && Input.GetButton("Jump"))
         {
             rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
         }
     }

     // We apply gravity manually for more tuning control
     rigidbody.AddForce(new Vector3(0, -gravity * rigidbody.mass, 0));

     grounded = false;
 }

 void OnCollisionStay()
 {
     grounded = true;
 }

 float CalculateJumpVerticalSpeed()
 {
     // From the jump height and gravity we deduce the upwards speed 
     // for the character to reach at the apex.
     return Mathf.Sqrt(2 * jumpHeight * gravity);
 }


RigBodyControl gets to the second one BotControlScript thru GetComponent

 public float animSpeed = 1.5f;                // a public setting for overall animator animation speed
 public float lookSmoother = 3f;                // a smoothing setting for camera motion
 //public bool useCurves;                        // a setting for teaching purposes to show use of curves

 
 private Animator anim;                            // a reference to the animator on the character
 private AnimatorStateInfo currentBaseState;            // a reference to the current state of the animator, used for base layer
 private AnimatorStateInfo layer2CurrentState;    // a reference to the current state of the animator, used for layer 2
 //private CapsuleCollider col;                    // a reference to the capsule collider of the character
 

 static int idleState = Animator.StringToHash("Base Layer.Idle");    
 static int locoState = Animator.StringToHash("Base Layer.Locomotion");            // these integers are references to our animator's states
 static int waveState = Animator.StringToHash("Layer2.Wave");
 

 void Start ()
 {
     // initialising reference variables
     anim = GetComponent<Animator>();                      
     //col = GetComponent<CapsuleCollider>();    
     if(anim.layerCount ==2)
         anim.SetLayerWeight(1, 1);
 }
 
 
 void FixedUpdate ()
 {
     float h = Input.GetAxis("Horizontal");                // setup h variable as our horizontal input axis
     float v = Input.GetAxis("Vertical");                // setup v variables as our vertical input axis
     anim.SetFloat("Speed", v);                            // set our animator's float parameter 'Speed' equal to the vertical input axis                
     anim.SetFloat("Direction", h);                         // set our animator's float parameter 'Direction' equal to the horizontal input axis        
     anim.speed = animSpeed;                                // set the speed of our animator to the public variable 'animSpeed'
     //currentBaseState = anim.GetCurrentAnimatorStateInfo(0);    // set our currentState variable to the current state of the Base Layer (0) of animation
     
     //if(anim.layerCount ==2)        
         //layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1);    // set our layer2CurrentState variable to the current state of the second Layer (1) of animation
     
         
         // Raycast down from the center of the character.. 
         //Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);
         //RaycastHit hitInfo = new RaycastHit();
         
         //if (Physics.Raycast(ray, out hitInfo))
         //{
             // ..if distance to the ground is more than 1.75, use Match Target
             //if (hitInfo.distance > 1.75f)
         //    {
                 
                 // MatchTarget allows us to take over animation and smoothly transition our character towards a location - the hit point from the ray.
                 // Here we're telling the Root of the character to only be influenced on the Y axis (MatchTargetWeightMask) and only occur between 0.35 and 0.5
                 // of the timeline of our animation clip
                 //anim.MatchTarget(hitInfo.point, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(0, 1, 0), 0), 0.35f, 0.5f);
             //}
         //}
     }
     

}

     // if we enter the waving state, reset the bool to let us wave again in future
     //if(layer2CurrentState.nameHash == waveState)
     //{
     //    anim.SetBool("Wave", false);
     //}

And some pics of my problem: WalkBack alt text

WalkForward alt text

walkback.png (378.5 kB)
walkforward.png (369.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Harardin · Aug 10, 2015 at 10:31 AM

Just need to reset collider and to change a Root transform position to center of mass and all starts to work correctly sorry for creating a question. At animation menu.alt text


unnamed.png (87.8 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image allenallenallen · Aug 10, 2015 at 10:32 AM 2
Share

No need to be sorry. If you've encountered such a problem, someone else probably will too. So it's good that you got a solution.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Problem with character movement 2d when attaching animator 1 Answer

Unity2D Sync falling object with animation 0 Answers

2D Animation does not start 1 Answer

Character Animation Help*! 1 Answer

Have some minor problems. Cant tell if its a script issue or a animator issue. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges