I wanted to create a SpeedBoost
once I enter a Collectable
but unfortunately it is not working.
Once I hit the collectable I get my runSpeed
. But my runSpeed
stays and doesn’t go back to moveSpeed
.
For more Information: I can access my moveSpeed
and my runSpeed
through the Unity Inspector and add a number that will be the move/run Speed of the Player. Before I made the SpeedBoost
I added runSpeed
on Shift so I can see that it works. I mainly Followed Tutorial on Udemy and tried to add assets etc. of my own.
This is the first question that I am asking so sorry if I didn’t explain something. I am new to Unity and just follow many tutorials. This is my whole Player Controller Script. The SpeedBoost
code starts at line 68 basically ends on line 90. But the boostTimer
and “boosting” added on start.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public Rigidbody2D rb;
public float jumpForce;
public float runSpeed;
private float activeSpeed;
public bool isGrounded;
public Transform groundCheckPoint;
public float groundCheckRadius;
public LayerMask whatIsGround;
public Animator ani;
private float boostTimer;
private bool boosting;
// Start is called before the first frame update
void Start()
{
boostTimer = 0;
boosting = false ;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatIsGround);
activeSpeed = moveSpeed;
if(Input.GetKey(KeyCode.LeftShift))
{
activeSpeed = runSpeed;
}
rb.velocity = new Vector2 ( Input.GetAxisRaw("Horizontal") * activeSpeed, rb.velocity.y);
if (Input.GetButtonDown("Jump"))
{
if(isGrounded == true)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
if(rb.velocity.x > 0)
{
transform.localScale = Vector3.one;
}
if (rb.velocity.x < 0)
{
transform.localScale = new Vector3(-1f, 1f, 1f);
}
//animation dings
ani.SetFloat("speed", Mathf.Abs(rb.velocity.x));
ani.SetBool("isGrounded", isGrounded);
ani.SetFloat("ySpeed", rb.velocity.y);
if (boosting)
{
boostTimer += Time.deltaTime;
if(boostTimer >= 3)
{
runSpeed = moveSpeed;
boostTimer = 0;
boosting = false;
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "SpeedBoost")
{
boosting = true;
moveSpeed = runSpeed;
Destroy(other.gameObject);
}
}
public void KnockBack()
{
rb.velocity = new Vector2(0f, jumpForce * .5f);
ani.SetTrigger("isHit");
}
}
Crexlo Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Assuming that:
activeSpeed
is the player’s current movement speed;
moveSpeed
is the player’s walking movement speed (constant);
runSpeed
is the player’s running movement speed (also constant);
I would assume that in OnTrigger2DSpeed
, activeSpeed
must be equal to runSpeed
, and not moveSpeed
– else you just told your code that walking speed is equal to running speed – and then in the next frame you set activeSpeed
to moveSpeed
that now is equal to running speed.