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 GameC12 · Sep 18, 2017 at 09:59 PM · animatorboolkeypress

If statement with holding key and bool active.

I have a character that has run and walk animations. I also have a bool for running and isWalking and use animator setbools. I have it set to activate running if isWalking and player is holding down R, but when I stop walking, it stops physical movement but not the animation for running, so how would I fix that. Code is below, thanks in advance.

 using UnityEngine;
 
 [RequireComponent(typeof(PlayerMotor))]
 public class PlayerController : MonoBehaviour
 {
     [SerializeField]
     private float baseSpeed = 5f;
     [SerializeField]
     private float runningSpeed = 10f;
 
     private float speed;
 
     [SerializeField]
     private float lookSensitivity = 3f;
 
     private bool running;
     public bool isWalking;
 
     private Animator anim;
     private PlayerMotor motor;
 
     void Start ()
     {
         motor = GetComponent<PlayerMotor>();
         anim = GetComponentInChildren<Animator> ();
 
         running = false;
     }
         
     void Update ()
     {
         float _xMov = Input.GetAxis ("Horizontal");
         float _zMov = Input.GetAxis ("Vertical");
 
         Vector3 _movHorizontal = transform.right * _xMov;
         Vector3 _movVertical = transform.forward * _zMov;
 
         Vector3 _velocity = (_movHorizontal + _movVertical) * speed;
 
         if (_zMov > 0.1)
             isWalking = true;
         else
             isWalking = false;
 
         anim.SetFloat ("walk", _zMov);
         anim.SetBool ("running", running);
 
         if (Input.GetKey (KeyCode.R) && isWalking == true) {
             running = true;
             speed = runningSpeed;
         } else {
             running = false;
             speed = baseSpeed;
         }
 
         motor.Move (_velocity);
 
         float _yRot = Input.GetAxisRaw ("Mouse X");
 
         Vector3 _rotation = new Vector3 (0f, _yRot, 0f) * lookSensitivity;
 
         motor.Rotate (_rotation);
 
 
         float _xRot = Input.GetAxisRaw ("Mouse Y");
 
         float _cameraRotationX = _xRot * lookSensitivity;
         motor.RotateCamera (_cameraRotationX);
 
         if (Input.GetKey (KeyCode.R))
             running = true;
     }
 
     void AnimationSpeed()
     {
         float animSpeed = 2;
         anim.speed = animSpeed;
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Hertex · Sep 18, 2017 at 10:21 PM

try this:

if(Input.GetKey (KeyCode.R)){ running = true; speed = runnigSpeed; } else { running = false; speed = baseSpeed; }

instead of:

if (Input.GetKey (KeyCode.R) && isWalking == true) { running = true; speed = runningSpeed; } else { running = false; speed = baseSpeed; }

and

if (Input.GetKey (KeyCode.R)) running = true;

it should work, you have the if statement for running twice and i guess that's the problem.

Good luck!

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 GameC12 · Sep 19, 2017 at 12:03 AM 0
Share

Never $$anonymous$$d, I started to mess around with some stuff, and I fixed it. I removed the bottom if statement, and then added in the isWalking bool again, and it worked. Thanks for your help!

avatar image
0

Answer by GameC12 · Sep 19, 2017 at 12:01 AM

First off, I am an idiot for not removing the if statement at the bottom, I put it in the first time I tried, and then moved it, but must have not deleted it. Second off, it still didn't work. Update Code: using UnityEngine; [RequireComponent(typeof(PlayerMotor))] public class PlayerController : MonoBehaviour { [SerializeField] private float baseSpeed = 5f; [SerializeField] private float runningSpeed = 10f; private float speed; [SerializeField] private float lookSensitivity = 3f; private bool running; public bool isWalking; private Animator anim; private PlayerMotor motor; void Start () { motor = GetComponent<PlayerMotor>(); anim = GetComponentInChildren<Animator> (); running = false; } void Update () { float _xMov = Input.GetAxis ("Horizontal"); float _zMov = Input.GetAxis ("Vertical"); Vector3 _movHorizontal = transform.right * _xMov; Vector3 _movVertical = transform.forward * _zMov; Vector3 _velocity = (_movHorizontal + _movVertical) * speed; if (_zMov > 0.1) isWalking = true; else isWalking = false; anim.SetFloat ("walk", _zMov); anim.SetBool ("running", running); if (Input.GetKey (KeyCode.R)) { running = true; speed = runningSpeed; } else { running = false; speed = baseSpeed; } motor.Move (_velocity); float _yRot = Input.GetAxisRaw ("Mouse X"); Vector3 _rotation = new Vector3 (0f, _yRot, 0f) * lookSensitivity; motor.Rotate (_rotation); float _xRot = Input.GetAxisRaw ("Mouse Y"); float _cameraRotationX = _xRot * lookSensitivity; motor.RotateCamera (_cameraRotationX); } }

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 GameC12 · Sep 19, 2017 at 12:01 AM 0
Share

That should make it less confusing.

using UnityEngine;

 [RequireComponent(typeof(Player$$anonymous$$otor))]
 public class PlayerController : $$anonymous$$onoBehaviour
 {
     [SerializeField]
     private float baseSpeed = 5f;
     [SerializeField]
     private float runningSpeed = 10f;
 
     private float speed;
 
     [SerializeField]
     private float lookSensitivity = 3f;
 
     private bool running;
     public bool isWalking;
 
     private Animator anim;
     private Player$$anonymous$$otor motor;
 
     void Start ()
     {
         motor = GetComponent<Player$$anonymous$$otor>();
         anim = GetComponentInChildren<Animator> ();
 
         running = false;
     }
         
     void Update ()
     {
         float _x$$anonymous$$ov = Input.GetAxis ("Horizontal");
         float _z$$anonymous$$ov = Input.GetAxis ("Vertical");
 
         Vector3 _movHorizontal = transform.right * _x$$anonymous$$ov;
         Vector3 _movVertical = transform.forward * _z$$anonymous$$ov;
 
         Vector3 _velocity = (_movHorizontal + _movVertical) * speed;
 
         if (_z$$anonymous$$ov > 0.1)
             isWalking = true;
         else
             isWalking = false;
 
         anim.SetFloat ("walk", _z$$anonymous$$ov);
         anim.SetBool ("running", running);
 
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.R)) {
             running = true;
             speed = runningSpeed;
         } else {
             running = false;
             speed = baseSpeed;
         }
 
         motor.$$anonymous$$ove (_velocity);
 
         float _yRot = Input.GetAxisRaw ("$$anonymous$$ouse X");
 
         Vector3 _rotation = new Vector3 (0f, _yRot, 0f) * lookSensitivity;
 
         motor.Rotate (_rotation);
 
 
         float _xRot = Input.GetAxisRaw ("$$anonymous$$ouse Y");
 
         float _cameraRotationX = _xRot * lookSensitivity;
         motor.RotateCamera (_cameraRotationX);
     }
 }
avatar image
0

Answer by Mihaere · Sep 19, 2017 at 12:15 AM

Also, if you haven't already in the PlayerMotor script, you should normalize your velocity or the player will move faster when using both axis (_xMov, _zMov) at the same time. But that's only if you haven't done this in the PlayerMotor script.

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

78 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

Related Questions

C# Acessing animator on another Object 1 Answer

Animator if Bool 1 Answer

Animator conditions, no bool state selector. 1 Answer

Animtor Stuck 1 Answer

Animator Bool: How do you use it? 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