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 xonin · Aug 04, 2019 at 08:40 AM · movement scriptnoob

Move Foward key W sometimes keeps walking after I let go of W, only happens afew times

Hi Guys,

I am fairly new to Unity, first time posting here.

I have an issue with my W to walk forward. There are usually 3 instances where I will press W, then let go and it will keep moving forward. It happens the very first time I press W, and then after I press W again it will go back to my idle/standing animation. After this first issue, it then works normally where it walks when I press W, and stops when I let go of W. It will then proceed to happen 1 or 2 more times at random after some more successful walking attempts.

I have pasted in my full script. I have been following this guide: https://www.youtube.com/watch?v=ReauId6jFFI

Thanks in advance :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     float speed = 4;
     float rotSpeed = 80;
     float rot = 0f;
     float gravity = 8;
 
     Vector3 moveDir = Vector3.zero;
 
     CharacterController controller;
     Animator anim;
 
     void Start()
     {
         controller = GetComponent<CharacterController>();
         anim = GetComponent<Animator>();
     }
 
     void Update()
     {
         Movement();
         GetInput();
     }
 
     void Movement()
     {
         if (controller.isGrounded)
         {
             if (Input.GetKey(KeyCode.W))
             {
                 if (anim.GetBool("attacking") == true)
                 {
                     return;
                 }
                 else if (anim.GetBool("attacking") == false)
                 {
                     anim.SetBool("walk", true);
                     anim.SetInteger("condition", 1);
                     moveDir = new Vector3(0, 0, 1);
                     moveDir *= speed;
                     moveDir = transform.TransformDirection(moveDir);
                 }
             }
             if (Input.GetKeyUp(KeyCode.W))
             {
                 anim.SetBool("walk", false);
                 anim.SetInteger("condition", 0);
                 moveDir = new Vector3(0, 0, 0);
             }
         }
         rot += Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
         transform.eulerAngles = new Vector3(0, rot, 0);
 
         moveDir.y -= gravity * Time.deltaTime;
         controller.Move(moveDir * Time.deltaTime);
     }
 
     void GetInput()
     {
         if (controller.isGrounded)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 if (anim.GetBool("walk") == true)
                 {
                     anim.SetBool("walk", false);
                     anim.SetInteger("condition", 0);
                 }
                 if (anim.GetBool("walk") == false)
                 {
                     Attacking();
                 }
             }
         }
     }
 
     void Attacking()
     {
         StartCoroutine(AttackRoutine());
     }
 
     IEnumerator AttackRoutine()
     {
         anim.SetBool("attacking", true);
         anim.SetInteger("condition", 2);
         yield return new WaitForSeconds(0.800F);
         anim.SetInteger("condition", 0);
         anim.SetBool("attacking", false);
     }
 }
 
 



 
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 xonin · Aug 04, 2019 at 10:00 AM

If anyone needs to know, I figured out that it is because the terrain my player is walking on is not completely flat. if (controller.isGrounded) does not like that at all. I tested this as on a flat plane my character movement works without an issue. If anyone has any advice on how to deal with this issue I would really appreciate it. Thank you.

Comment
Add comment · Show 3 · 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 Dragate · Aug 04, 2019 at 10:23 AM 1
Share

I'd suggest moving

 if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.W)){
    // ...
 }

out of

 if (controller.isGrounded){
    // ...
 }
avatar image xonin Dragate · Aug 06, 2019 at 04:10 AM 0
Share

Hi there, at first I misunderstood your reply but I realise now that it looks like it should work! Thanks for your reply, appreciate it! I will let you know how it goes when I test it out tonight :)

avatar image xonin Dragate · Aug 06, 2019 at 10:17 AM 1
Share

Hello again, just want to say THAN$$anonymous$$S SO $$anonymous$$UCH! I just adjusted it as you suggested and it works a charm!

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

114 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

Related Questions

NPCs to look in the direction the target is 0 Answers

Sorry if this is reposted but it seems like the website didn't post it before. How could I set up a system to kill players when they touch specific meshes and send them to a checpoint? 0 Answers

removing player duplicates after scene transition 2 Answers

If i push an object with rigidbody why does it float away even tough i have a force going forward 0 Answers

ShaderGraph Multiple RenderTargets 0 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