Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Mar 08, 2021 at 02:05 AM by iR3dyPlayZ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by iR3dyPlayZ · Mar 07, 2021 at 06:12 AM · 2dinput2d-physicsnormalized

Diagonal movement

I'm working on a 2d Game where my character can dodge via sliding... when I normalize my input I'm not able to dodge in the Up Left, Dow Left, Dow Right... but I'm able to dodge Up right... and I can also dodge on the normal 4 axis. can someone please explain this strange issue and help me correct it thank you!

 public void HandleMovement()
      {
          input.x = Input.GetAxisRaw("Horizontal");
          input.y = Input.GetAxisRaw("Vertical");
          input = input.normalized;
         
  
          animator.SetFloat("Speed", input.sqrMagnitude);
  
          bool isIdle = input.x == 0 && input.y == 0;
          if (isIdle)
          {
              animator.SetFloat("LastInputX", lastInput.x);
              animator.SetFloat("LastInputY", lastInput.y);
          }
          else
          {
              playerRigidbody.MovePosition(transform.position + input * walk * Time.deltaTime);
              animator.SetFloat("Horizontal", input.x);
              animator.SetFloat("Vertical", input.y);
              lastInput = input;
          }
      }
  
      private void HandleDodging()
      {
          if (Input.GetKeyDown(KeyCode.Space) && input.x == 1 | input.y == 1 | input.x == -1 | input.y == -1)
          {
              state = State.Dodging;
              isDodging = true;
          }
      }
  
      private void Dodging()
      {
          playerRigidbody.MovePosition(transform.position + input * dodgeSpeed * Time.deltaTime);
          dodgeSpeed -= dodgeSpeed * dodgeSpeedReduced * Time.deltaTime;
  
          if (dodgeSpeed < dodgeSpeedMinimum)
          {
              state = State.Normal;
              dodgeSpeed = dodge;
              isDodging = false;
          }
      }

Comment
Add comment · Show 12
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 Drayanlia · Mar 07, 2021 at 11:17 AM 1
Share

First I would advise you to put all physics related code in FixedUpdate() and to change your Time.deltaTime by Time.fixedDeltaTime.
Then I suspect you are trying to call HandleMovement() and Dodging in the same frame which means only the last playerRigidbody.MovePosition will resolve. Would you $$anonymous$$d sharing the trunk of code where you call all these methods ?

avatar image iR3dyPlayZ Drayanlia · Mar 07, 2021 at 11:31 AM 0
Share

I definitely will when I wake up, not by the computer atm, I will also make those corrections and post it up for you thank you for going out of your way to help me hopefully you're on the afternoon when I awake!

avatar image iR3dyPlayZ Drayanlia · Mar 07, 2021 at 08:55 PM 0
Share

I'm awake now, sorry hope you're still available.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody2D))]
 [RequireComponent(typeof(BoxCollider2D))]
 public class PlayerController : MonoBehaviour
 {
     public Animator animator;
     private Rigidbody2D playerRigidbody;
 
     private Vector3 input;
     private Vector3 lastInput;
 
     public float walkSpeed;
     float walk;
 
     public float dodgeSpeed;
     float dodge;
     public float dodgeSpeedReduced;
     float dodgeSpeedMinimum;
     public bool isDodging;
     
 
     private State state;
     private enum State
     {
         Normal, Dodging
     }
 
     public void Awake()
     {
         playerRigidbody = GetComponent<Rigidbody2D>();
         walk = walkSpeed;
         state = State.Normal;
         dodgeSpeedMinimum = walk;
         dodge = dodgeSpeed;
     }
 
     private void Update()
     {
         HandleMovement();
     }
 
     public void FixedUpdate()
     {
         switch (state)
         {
             case State.Normal:
                 HandleDodging();
                 break;
             case State.Dodging:
                 Dodging();
                 break;
         }
     }
 
     public void HandleMovement()
     {
         input.x = Input.GetAxisRaw("Horizontal");
         input.y = Input.GetAxisRaw("Vertical");
         input = input.normalized;
 
         animator.SetFloat("Speed", input.sqrMagnitude);
 
         bool isIdle = input.x == 0 && input.y == 0;
         if (isIdle)
         {
             animator.SetFloat("LastInputX", lastInput.x);
             animator.SetFloat("LastInputY", lastInput.y);
         }
         else
         {
             playerRigidbody.MovePosition(transform.position + input * walk * Time.fixedDeltaTime);
             animator.SetFloat("Horizontal", input.x);
             animator.SetFloat("Vertical", input.y);
             lastInput = input;
         }
     }
 
     private void HandleDodging()
     {
         if (Input.GetKeyDown(KeyCode.Space) )
         {
             state = State.Dodging;
 
             isDodging = true;
         }
     }
 
     private void Dodging()
     {
         playerRigidbody.MovePosition(transform.position + input * dodgeSpeed * Time.fixedDeltaTime);
         dodgeSpeed -= dodgeSpeed * dodgeSpeedReduced * Time.deltaTime;
 
         if (dodgeSpeed < dodgeSpeedMinimum)
         {
             state = State.Normal;
             dodgeSpeed = dodge;
             isDodging = false;
         }
     }
 }
 
avatar image iR3dyPlayZ Drayanlia · Mar 07, 2021 at 09:00 PM 0
Share

In this code with out && input.x == 1..... which is after the input. get keycode space. you can move upright diagonal but no other one

avatar image Drayanlia iR3dyPlayZ · Mar 07, 2021 at 09:33 PM 1
Share

Thank you. So in the code you shared you should not use playerRigidBody.Moveposition in Update(), try moving into HandleDodging like that :


 private void HandleDodging()
 {
     bool isIdle = input.x == 0 && input.y == 0;
 
     if (Input.GetKeyDown(KeyCode.Space) & (input.x == 1 | input.y == 1 | input.x == -1 | input.y == -1))
     {
         state = State.Dodging;
 
         isDodging = true;
     }
     else if (!isIdle)
     {
         playerRigidbody.MovePosition(transform.position + input * walk * Time.fixedDeltaTime);
     }
 }


Get rid of input = input.normalized in HandleMovement()

Show more comments

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by iR3dyPlayZ · Mar 07, 2021 at 11:59 PM

@Drayanlia if you want to give up on me I understand but it's still not working and its not checking for space in the bool.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody2D))]
 [RequireComponent(typeof(BoxCollider2D))]
 public class PlayerController : MonoBehaviour
 {
     public Animator animator;
     private Rigidbody2D playerRigidbody;
 
     private Vector3 input;
     private Vector3 lastInput;
 
     public float walkSpeed;
     float walk;
 
     public float dodgeSpeed;
     float dodge;
     public float dodgeSpeedReduced;
     float dodgeSpeedMinimum;
     public bool isDodging;
 
     public bool spacePressed;
 
     private State state;
     private enum State
     {
         Normal, Dodging
     }
 
     public void Awake()
     {
         playerRigidbody = GetComponent<Rigidbody2D>();
         walk = walkSpeed;
         state = State.Normal;
         dodgeSpeedMinimum = walk;
         dodge = dodgeSpeed;
     }
 
     private void Update()
     {
         HandleMovement();
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             spacePressed = true;
         }
     }
 
     public void FixedUpdate()
     {
         switch (state)
         {
             case State.Normal:
                 HandleDodging();
                 break;
             case State.Dodging:
                 Dodging();
                 break;
         }
         spacePressed = false;
     }
 
     public void HandleMovement()
     {
         input.x = Input.GetAxisRaw("Horizontal");
         input.y = Input.GetAxisRaw("Vertical");


 
         animator.SetFloat("Speed", input.sqrMagnitude);
         animator.SetFloat("LastInputX", lastInput.x);
         animator.SetFloat("LastInputY", lastInput.y);
 
         animator.SetFloat("Horizontal", input.x);
         animator.SetFloat("Vertical", input.y);
     }
 
     private void HandleDodging()
     {
         bool isIdle = input.x == 0 && input.y == 0;
 
         if (spacePressed & (input.x == 1 | input.y == 1 | input.x == -1 | input.y == -1))
         {
             state = State.Dodging;
             isDodging = true;
         }
         else if (!isIdle)
         {
             playerRigidbody.MovePosition(transform.position + input.normalized * walk * Time.fixedDeltaTime);
 
             lastInput = input;
         }
     }
 
     private void Dodging()
     {
         playerRigidbody.MovePosition(transform.position + input * dodgeSpeed * Time.fixedDeltaTime);
         dodgeSpeed -= dodgeSpeed * dodgeSpeedReduced * Time.deltaTime;
 
         if (dodgeSpeed < dodgeSpeedMinimum)
         {
             state = State.Normal;
             dodgeSpeed = dodge;
             isDodging = false;
         }
     }
 }

Comment
Add comment · Show 11 · 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 Drayanlia · Mar 08, 2021 at 12:11 AM 2
Share

Ok first let's fix checking for space ! Add this in FixedUpdate() before you set spacePressed to false

 if (spacePressed)
 {
     Debug.Log("Space bar was pressed.");
 }

Run some tests and tell me if it's printing the message in the console. Verify if it appears every time you press the key and only once by press.

avatar image iR3dyPlayZ Drayanlia · Mar 08, 2021 at 12:15 AM 0
Share

all 4 directions plus upright but not the other 3 diagonals

avatar image iR3dyPlayZ Drayanlia · Mar 08, 2021 at 12:18 AM 0
Share

even when stationary it checks it

avatar image Drayanlia iR3dyPlayZ · Mar 08, 2021 at 12:22 AM 0
Share

Can you show me your FixedUpdate again ?

Show more comments

Follow this Question

Answers Answers and Comments

296 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

Related Questions

Cannot set motor speed in script 1 Answer

How do I fix sticking to walls while still allowing friction on the ground? 1 Answer

How to convert Torque Stabilizer to RigidBody2D? 1 Answer

Ball bounces at different heights when it shouldn't 0 Answers

Edit tilemap cillider edit z hight 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