Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RobAnthem · Feb 19, 2019 at 05:30 PM · rotationanimatorcontrollerik

Keeping 4-Legged entity grounded

INITIAL PROBLEM: Unable to keep all 4 feet grounded on sloped terrain. IK is build for humanoids not creatures with 4 legs. I've tried various methods to solve it, nothing really great.

UPDATE 1: So far this is my solution, also I should mention I included this in my root behaviour system because all my animals are missing root motion, and I added this via curve. Also was hoping to tie in an IK solution here but that didn't work out. SO I defined the front and back of the entity and attempted to make sure it stayed... contoured lol. Seriously a janky solution, doesn't look bad. Also was getting erroneous behaviour with the Z axis and sometimes the X axis was going to far in a single direction, so I clamped the X to a float+- and clamped Z to zero , but unfortunately this means the animal can never contour at an angle that's left or rift of the animal. Luckily this game isn't AAA so it it looks fine as it, but this solution is terrible in my professional opinion.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Animator))]
 
 public class RootMotionController : MonoBehaviour
 {
     private Animator animator;
     private MLSpace.NPCScript npc;
     public Vector2 forwardAxisClamp = new Vector2(-20, 20);
     public float forwardRotationSpeed = 30.0f;
     public float RootMotionSpeedModifier = 1.0f;
     public float footGroundDistance = 0.05f;
     public float FootCheckDistance = 0.5f;
     private const float fullCircle = 360f;
     void Awake()
     {
         npc = GetComponent<MLSpace.NPCScript>();
         animator = GetComponent<Animator>();
     }
     public Transform frontTransform, backTransform;
     void OnAnimatorMove()
     {
 
         if (animator)
         {
             Vector3 newPosition = transform.position;
             newPosition += transform.forward *( animator.GetFloat("Motion") * RootMotionSpeedModifier) * Time.smoothDeltaTime;
             transform.position = newPosition;
         }
     }
     void Update()
     {
         if (!npc.isDead)
         {
             Ray ray = new Ray(frontTransform.position, Vector3.down);
             Ray ray2 = new Ray(backTransform.position, Vector3.down);
             RaycastHit hit;
             RaycastHit hit2;
             Physics.Raycast(ray, out hit, FootCheckDistance);
             Physics.Raycast(ray2, out hit2, FootCheckDistance);
             if (hit.distance > hit2.distance)
             {
                 if (hit.distance > footGroundDistance)
                 {
                     transform.Rotate(forwardRotationSpeed * Time.deltaTime, 0, 0);
                 }
             }
             else
             {
                 if (hit2.distance > footGroundDistance)
                 {
 
                     transform.Rotate(-forwardRotationSpeed * Time.deltaTime, 0, 0);
                 }
             }
             Vector3 rot = transform.localEulerAngles;
             rot.z = 0;
             if (rot.x < fullCircle/2)
             {
                 if (rot.x > forwardAxisClamp.y)
                 {
                     rot.x = forwardAxisClamp.y;
                 }
             }
             else
             {
                 if (rot.x < fullCircle + forwardAxisClamp.x)
                 {
                     rot.x = fullCircle + forwardAxisClamp.x;
                 }
             }
             transform.localEulerAngles = rot;
         }
     }
 }

 

Also it should be noted that the fullCircle modifier is because localEulers don't go below zero, the revert back to 360 and move down, if above 360, they move to 1 and greater. My simples solution was that if it was <180 degrees, it was obviously on the 1 and great side of things, and if it was above 180 degrees it was on the 360 and less degrees.

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

0 Replies

· Add your reply
  • Sort: 

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

155 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IK for hands 0 Answers

Joystick for WASD & Joystick for Camera? 0 Answers

Can't assign Animator Override Controller 0 Answers

Best way to implement the working of an Articulated Work Platform (AWP) or Boom Lift 1 Answer

How do I add the animation to my player in Unity 4? 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