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
Your answer
Follow this Question
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
Make object follow another object while keeping the same distance from it 1 Answer