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 /
  • Help Room /
avatar image
0
Question by Imamul125 · Oct 05, 2018 at 01:15 AM · animationerrorif-statementsblend tree3rd person camera

can anyone help me out what is wrong with this code?

i have written a code to allow my player to animate according to 1d blend tree. everything is fine. but as soon as i use Lshift in my script to animate run. the animation never fade back to zero(ideal). i want to player to animate walk animation when "horizontal and vertical button" are pressed. and use run animation when "horizontal and vertical button + leftshit" is pressed

 ![public class movement_rotation : MonoBehaviour
 {
 
     public float InputX;
     public float InputZ;
     public Vector3 desiredMoveDirection;
     public bool blockRotationPlayer;
     public float desiredRotationSpeed;
     public Animator anim;
     public float Speed;
     public float allowPlayerRotation;
     public Camera cam;
     public CharacterController controller;
     public bool isGrounded;
     private float verticalVel;
     private Vector3 moveVector;
     public float walkSpeed;
     public float runspeed;
     public bool Lshift;
     
 
 
     // Use this for initialization
     void Start()
     {
         anim = this.GetComponent<Animator>();
         cam = Camera.main;
         controller = this.GetComponent<CharacterController>();
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
         
         InputMagnitude();
 
 
         isGrounded = controller.isGrounded;
         if (isGrounded)
         {
             verticalVel -= 0;
         }
         else
         {
             verticalVel -= 2;
         }
         moveVector = new Vector3(0, verticalVel, 0);
 
     }
 
     void PlayerMoveAndRotation()
     {
         InputX = Input.GetAxis("Horizontal");
         InputZ = Input.GetAxis("Vertical");
 
 
         var camera = Camera.main;
         var forward = cam.transform.forward;
         var right = cam.transform.right;
 
         forward.y = 0f;
         right.y = 0f;
 
         forward.Normalize();
         right.Normalize();
 
         desiredMoveDirection = forward * InputZ + right * InputX;
 
         if (blockRotationPlayer == false)
         {
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(desiredMoveDirection), desiredRotationSpeed);
 
         }
 
     }
 
     void InputMagnitude()
     {
         Lshift = Input.GetKey(KeyCode.LeftShift);
         //calculate input
         InputX = Input.GetAxis("Horizontal");
         InputZ = Input.GetAxis("Vertical");
 
 
         anim.SetFloat("InputZ", InputZ, 0.0f, Time.deltaTime * 2f);  //from animator float of InputX is called
         anim.SetFloat("InputX", InputX, 0.0f, Time.deltaTime * 2f); //from animator float of InputX is called
 
         //calculate the input magnitude
         Speed = new Vector2(InputX, InputZ).sqrMagnitude;
 
 
         if (Speed > allowPlayerRotation)
         {
 
             if (Lshift)
             {
                 transform.Translate(Vector3.forward * Speed * runspeed * Time.deltaTime);
                 anim.SetFloat("InputMagnitude", Speed * 1.0f, 0.0f, Time.deltaTime);
                 PlayerMoveAndRotation();
             }
             else if (Lshift = false)
             {
                 transform.Translate(Vector3.forward * Speed * walkSpeed * Time.deltaTime);
                 anim.SetFloat("InputMagnitude", Speed * 0.5f, 0.0f, Time.deltaTime);
                 PlayerMoveAndRotation();
 
             }
 
 
 
             
         }
 
         else if (Speed < allowPlayerRotation)
         {
             anim.SetFloat("InputMagnitude", Speed, 0.0f, Time.deltaTime);
         }
     }
         
      
       
 }][1]


[1]: /storage/temp/125627-screenshot-870.png

screenshot-870.png (176.2 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
1
Best Answer

Answer by Bunny83 · Oct 05, 2018 at 01:33 AM

This line (#103) does not check if Lshift is false but it actually sets it to false.

 else if (Lshift = false)

if should be

 else if (Lshift == false)

or just

 else 

Since your first if statement is already the inverse condition. I don't know if there are more issues since you posted quite a bit of code.

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 Imamul125 · Oct 05, 2018 at 01:43 AM 0
Share

no it was the only error.. thanks a lot. :) but i have problem. when i press Leftshift after vertical key . the value of Input$$anonymous$$agnitude just snap to value 1, ins$$anonymous$$d of increasing to 1.. but this is not the case when leftshift is pressed before vertical key.

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

264 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

Related Questions

The name animator does not exist in the current context... Help? 1 Answer

Play animation on mouse click 1 Answer

.IsPlaying does not exist? 1 Answer

how do I use an Idle animation clip in a 2d blend tree? 0 Answers

Help with bools and if else statements. Animations 2 Answers


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