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 acer777 · Aug 02, 2019 at 11:19 AM · rotationcontrollerplayer movementtopdown

Player Movement & Blend Tree - Can anyone help with my script?

So I have my blend tree setup with idle in center, walking, running for all directions strafe left, right etc. It's a top down game using both analog sticks, the left moves the in the direction and the right rotates them to face the sticks direction. The system walks apart from I can slightly move the left stick to walk as no matter how much force I push on the left stick it always returns -1 or 1 and not 0.5 or -0.5 for walking animations? My script is below does anyone have any idea what I'm doing wrong here? I've hit that coding break wall and smack my head against it hard!

Any help would be massively appreciated!

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class playerMovement : MonoBehaviour {
  
      public GameObject model;
      public Animator animator;
      private Vector2 LeftAxis;
      private Vector2 RightAxis;
      
  private Vector3 lastLeftStickInputAxis;  // stores the axis input from the left stick between frames
  
  private void Update()
  {
      /* START LOCOMOTION */
  
      LeftAxis = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
  
      RightAxis = new Vector2(Input.GetAxis("Horizontal2"), Input.GetAxis("Vertical2"));
  
      // Get the axis from the left stick (a Vector2 with the left stick's direction)
      var leftStickInputAxis = LeftAxis;
      
      // Get the angle between the the direction the model is facing and the input axis vector
      var a = SignedAngle(new Vector3(leftStickInputAxis.x, 0, leftStickInputAxis.y), model.transform.forward);
      
      // Normalize the angle
      if (a < 0)
      {
          a *= -1;
      }
      else
      {
          a = 360 - a;
      }
      
      // Take into consideration the angle of the camera
      //a += Camera.main.transform.eulerAngles.y;
  
      var aRad = Mathf.Deg2Rad*a; // degrees to radians
      Debug.Log(leftStickInputAxis);
      // If there is some form of input, calculate the new axis relative to the rotation of the model
      if (leftStickInputAxis.x != 0 || leftStickInputAxis.y != 0)
      {
          leftStickInputAxis = new Vector2(Mathf.Sin(aRad), Mathf.Cos(aRad));
      }
      
      float xVelocity = 0f, yVelocity = 0f;
      float smoothTime = 0.05f;
  
      // Interpolate between the input axis from the last frame and the new input axis we calculated
      leftStickInputAxis = new Vector2(Mathf.SmoothDamp(lastLeftStickInputAxis.x, leftStickInputAxis.x, ref xVelocity, smoothTime), Mathf.SmoothDamp(lastLeftStickInputAxis.y, leftStickInputAxis.y, ref yVelocity, smoothTime));
      
      // Update the Animator with our values so that the blend tree updates
      animator.SetFloat("VelX", leftStickInputAxis.x);
      animator.SetFloat("VelZ", leftStickInputAxis.y);
      
      lastLeftStickInputAxis = leftStickInputAxis;
  
      /* END LOCOMOTION */
  
  
  
      /* START ROTATION */
  
      // Get the axis from the right stick (a Vector2 with the right stick's direction)
      var rightStickInputAxis = RightAxis; 
      if (rightStickInputAxis.x != 0 || rightStickInputAxis.y != 0)
      {
          float angle2 = 0;
          if (rightStickInputAxis.x != 0 || rightStickInputAxis.y != 0)
          {
              angle2 = Mathf.Atan2(rightStickInputAxis.x, rightStickInputAxis.y)*Mathf.Rad2Deg;
              if (angle2 < 0)
              {
                  angle2 = 360 + angle2;
              }
          }
      
          // Calculate the new rotation for the model and apply it
          var rotationTo = Quaternion.Euler(0, angle2 + Camera.main.transform.eulerAngles.y, 0);
          model.transform.rotation = Quaternion.Slerp(model.transform.rotation, rotationTo, Time.deltaTime*10);
      }
  
      /* END ROTATION */
  }
  
  private float SignedAngle(Vector3 a, Vector3 b)
  {
      return Vector3.Angle(a, b) * Mathf.Sign(Vector3.Cross(a, b).y);
  }
  }
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

Answer by StalwartList · Oct 26, 2020 at 11:21 AM

Hello this is my first time posting but i also ran into this problem.

the way you fix this is by multiplying your left stick axis by the current left stick velocity. for example.

 velocity = Mathf.Abs(LeftAxis.x) + Mathf.Abs(LeftAxis.y);
 
 animator.SetFloat("VelX", leftStickInputAxis.x * velocity);
 animator.SetFloat("VelZ", leftStickInputAxis.y * velocity);

Comment
Add comment · 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

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

148 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

Related Questions

[New Input System] Player Rotation & Cursor Following Mouse,[New Input System] Player Rotation & Cursor Object Following Mouse 0 Answers

Need help rotating child of player based off of player movement. 1 Answer

Topdown player movement with controller problem 1 Answer

Top down game. Facing direction problem. 1 Answer

Hi, knife hit, how do you control the speed of the wheel and the speed of the knife? 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