Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Sakref · Mar 17 at 05:26 PM · animationnewbie

how to I use 2d directional animator with the new input system?

Hello Everyone. I am currently working on using the new input system package and I've made a player character than move rather basic. but I wanted to expand the controls with more animations depending on the direction you move (like strafing left or right). right now the character can move forward and the animations are on a 1d blend type. but wanted to use the 2D blend types to have more variation of animations depending on how you move. this is the class for the animations: public class ThirdPersonAnimation : MonoBehaviour { private Animator animator; private Rigidbody rb; private float maxSpeed = 5f;

     // Start is called before the first frame update
     void Start()
     {
         animator = this.GetComponent<Animator>();
         rb = this.GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
         animator.SetFloat("speed", rb.velocity.magnitude / maxSpeed);
     }
 }

and here is how I use the input system: public class ThirdPersonController : MonoBehaviour { //input fields private ThirdPersonActionsAsset playerActionsAsset; private InputAction move;

 //movement fields
 private Rigidbody rb;
 [SerializeField]
 private float movementForce = 1f;
 [SerializeField]
 private float jumpForce = 5f;
 [SerializeField]
 private float maxSpeed = 5f;
 private Vector3 forceDirection = Vector3.zero;

 [SerializeField]
 private Camera playerCamera;
 private Animator animator;

 private void Awake()
 {
     rb = this.GetComponent<Rigidbody>();
     playerActionsAsset = new ThirdPersonActionsAsset();
     animator = this.GetComponent<Animator>();
 }

 private void OnEnable()
 {
     playerActionsAsset.Player.Jump.started += DoJump;
     move = playerActionsAsset.Player.Move;
     playerActionsAsset.Player.Enable();
 }

 private void OnDisable()
 {
     playerActionsAsset.Player.Jump.started -= DoJump;
     playerActionsAsset.Player.Disable();
 }

 private void FixedUpdate()
 {
     forceDirection += move.ReadValue<Vector2>().x * GetCameraRight(playerCamera) * movementForce;
     forceDirection += move.ReadValue<Vector2>().y * GetCameraForward(playerCamera) * movementForce;

     rb.AddForce(forceDirection, ForceMode.Impulse);
     forceDirection = Vector3.zero;

     if (rb.velocity.y < 0f)
         rb.velocity -= Vector3.down * Physics.gravity.y * Time.fixedDeltaTime;

     Vector3 horizontalVelocity = rb.velocity;
     horizontalVelocity.y = 0;
     if (horizontalVelocity.sqrMagnitude > maxSpeed * maxSpeed)
         rb.velocity = horizontalVelocity.normalized * maxSpeed + Vector3.up * rb.velocity.y;

     LookAt();
 }

 private void LookAt()
 {
     Vector3 direction = rb.velocity;
     direction.y = 0f;

     if (move.ReadValue<Vector2>().sqrMagnitude > 0.1f && direction.sqrMagnitude > 0.1f)
         this.rb.rotation = Quaternion.LookRotation(direction, Vector3.up);
     else
         rb.angularVelocity = Vector3.zero;
 }

 private Vector3 GetCameraForward(Camera playerCamera)
 {
     Vector3 forward = playerCamera.transform.forward;
     forward.y = 0;
     return forward.normalized;
 }

 private Vector3 GetCameraRight(Camera playerCamera)
 {
     Vector3 right = playerCamera.transform.right;
     right.y = 0;
     return right.normalized;
 }

 private void DoJump(InputAction.CallbackContext obj)
 {
     if (IsGrounded())
     {
         forceDirection += Vector3.up * jumpForce;
     }
 }

 private bool IsGrounded()
 {
     Ray ray = new Ray(this.transform.position + Vector3.up * 0.25f, Vector3.down);
     if (Physics.Raycast(ray, out RaycastHit hit, 0.3f))
         return true;
     else
         return false;
 }

if there is need for more info you are welcome to ask.

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

320 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does my climb stairs animation not play? 1 Answer

I try'd so hard, but my animation script doesnt work..? 1 Answer

Can the animation editor create local rotational data? 3 Answers

Adding animation clips via script 2 Answers

Animation Input / ETC 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