• 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 azuzster · Apr 08, 2020 at 07:34 AM · vector3transform.positionvector3.lerp

Move Character from one position to another position using Key, without changing its speed over-time.

Hello, I'm trying to make a fighting game and I use only 2 keys. I can change the position of my player but every time I reuse the key combination it moves faster. Please help me fix this annoying bug, thanks.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerScript : MonoBehaviour
 {
 
     // LINK TO H'S ANIMATOR
     Animator h_Animator;
 
     // LINK TO H'S SPRITE RENDERER
     SpriteRenderer h_SpriteRenderer;
 
     // H'S ATTACK TIMER
     float attackTimer = 0.0f;
 
     // H ATTACK VARIBLES
     bool doubleSlash = false;
     bool dashStrike = false;
     float currentPos = 0.0f;
     public float transitionSpeed = 0.1f;
 
 
     IEnumerator AttackRight()
     {
         while (true)
         {
             if (transform.position.x <= 3.0f)
             {
                 Vector3 newPos = new Vector3(currentPos, transform.position.y, transform.position.z);
                 transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime * transitionSpeed);
             }
             yield return null;
         }
     }
 
     IEnumerator AttackLeft()
     {
         while (true)
         {
             if (transform.position.x >= -3.0f)
             {
                 Vector3 newPos = new Vector3(currentPos, transform.position.y, transform.position.z);
                 transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime * transitionSpeed);
             }
             yield return null;
         }
     }
 
     // Start is called before the first frame update
     void Start()
     {
         // FETCH H'S ANIMATOR
         h_Animator = GetComponent<Animator>();
 
         // FETCH H'S SPRITE RENDERER
         h_SpriteRenderer = GetComponent<SpriteRenderer>();
     }
 
     // Update is called once per frame
     void Update()
     {
 
         // H [attackTimer] WILL CHECK IF ATTACK PERSIST
         if (attackTimer <= 0.0f)
         {
             doubleSlash = false;
             dashStrike = false;
             //print("DoubleSlash: " + doubleSlash);
             //print("DashStrike: " + dashStrike);
             attackTimer = 0.0f;
             //print(attackTimer);
             h_Animator.SetInteger("State", 2);
         }
         else
         {
             attackTimer -= Time.deltaTime;
             //print(attackTimer);
         }
 
         // RIGHT ATTACK
         if (attackTimer <= 0.0f && Input.GetKeyDown(KeyCode.Slash))
         {
             
             // DO NOT FLIP SPRITE
             h_SpriteRenderer.flipX = false;
             //print(transform.position.x);
 
             // DASH STRIKE ATTACK
             if (transform.position.x <= 0.0f)
             {
                 currentPos = 3.0f;
                 dashStrike = true;
                 //print("DashStrike: " + dashStrike);
                 attackTimer = 0.88f; // SET [attackTimer] VAR TO SPECIFIC TIME
                 h_Animator.SetInteger("State", 4);
                 StartCoroutine("AttackRight");
             }
 
             // DOUBLE SLASH ATTACK
             else
             {
                 doubleSlash = true;
                 //print("DoubleSlash: " + doubleSlash);
                 attackTimer = 0.66f; // SET [attackTimer] VAR TO SPECIFIC TIME
                 h_Animator.SetInteger("State", 3);
             }
         }
 
         // LEFT ATTACK
         if (attackTimer <= 0.0f && Input.GetKeyDown(KeyCode.Z))
         {
             // FLIP SPRITE
             h_SpriteRenderer.flipX = true;
             //print(transform.position.x);
 
             // DASH STRIKE ATTACK
             if (transform.position.x >= 0.0f)
             {
                 currentPos = -3.0f;
                 dashStrike = true;
                 //print("DashStrike: " + dashStrike);
                 attackTimer = 0.88f; // SET [attackTimer] VAR TO SPECIFIC TIME
                 h_Animator.SetInteger("State", 4);
                 StartCoroutine("AttackLeft");
             }
 
             // DOUBLE SLASH ATTACK
             else
             {
                 doubleSlash = true;
                 //print("DoubleSlash: " + doubleSlash);
                 attackTimer = 0.66f; // SET [attackTimer] VAR TO SPECIFIC TIME
                 h_Animator.SetInteger("State", 3);
             }
         }
     }
 }


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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

226 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

Related Questions

Are I stupid? Vector3 1 Answer

3D Pong Clone: Set position of a Rigidbody, or change how a speed vector is verified? 1 Answer

Move GameObject to Point A to B 1 Answer

Gameobjects transform position minus range 1 Answer

I want my props to be in front of my camera even when my camera is rotating ,How to set a GameObject's position in front of a FPSController 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