- Home /
How do i increase Speed while running 2d
Yoh guys, i have two issues, the first is that when i run even if I release the key (R) the character keeps running, the second is that I can not grow the speed: so both when walking and when running, the speed remains the same. Someone can help me?
public class Movement : MonoBehaviour {
public float speedmultiplier = 100.0f;
public float runSpeed = 50f;
public Rigidbody2D rb2d;
private float currentMaxSpeed;
bool facingRight = true;
public float speed= 10f;
public Animator animator;
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator> ();
rb2d = GetComponent<Rigidbody2D> ();
}
void Update ()
{
if (Input.GetKey (KeyCode.D))
{
transform.Translate (new Vector2 (1, rb2d.velocity.y) * Time.deltaTime * speedmultiplier);
}
else if (Input.GetKey (KeyCode.A))
{
transform.Translate (new Vector2 (-1, rb2d.velocity.y) * Time.deltaTime * speedmultiplier);
}
if (Input.GetAxis ("Horizontal") > 0 && !facingRight)
{
rb2d.AddForce (transform.right * speedmultiplier);
Flip ();
animator.Play ("Walk");
}
if (Input.GetAxis ("Horizontal") < 0 && facingRight)
{
rb2d.AddForce (transform.right * speedmultiplier);
Flip ();
animator.Play ("Walk");
}
else if (Input.GetAxisRaw ("Horizontal") == 0)
{
rb2d.velocity = new Vector2 (0, rb2d.velocity.y);
animator.Play ("Idle");
}
if (Input.GetKey (KeyCode.R))
{
animator.speed = 30f;
animator.Play ("Run");
bool Run = true;
rb2d.AddForce (transform.right * speedmultiplier);
}
float speed = currentMaxSpeed * Input.GetAxis ("Horizontal");
animator.SetFloat ("speed", Mathf.Abs (speed));
transform.Translate (Vector2.right * speed * Time.deltaTime);
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Answer by AmirSavand · Feb 25, 2018 at 04:36 PM
You need to have a variable for changing speed when running, keep the variable at 1f and multiply it by the final force you give the rigidbody for speed, then if you increase that value like make it 2f, the speed will be 2x faster.
Can you please give an example trough the script? Never done it and i am really a beginner..
Well I can not code for you, but you need to increase the velocity, that's it, that's all you need o do.
Your code is pretty messy too.
Don't use key codes, use input names from player inputs. Don't use condition on facing left or right.
Try looking up some tutorials or wait for someone to write code for you.
I'm making a 3d infinite runner and my characters speed increments everytime he LevelsUp. but the problem is that he doesn't gradually increment, ins$$anonymous$$d, he goes from normal to very fast and its hard to control him. here's my code:
public class PlayerController : $$anonymous$$onoBehaviour
{
private CharacterController controller;
private float speed = 2.0f;
private Vector3 moveVector;
private float vertivalVelocity = 0.0f;
private float gravity = 12.0f;
private float animationDuration = 2.0f;
private bool isDead = false;
// Use this for initialization
void Start ()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update ()
{
if (isDead)
return;
if(Time.time < animationDuration)
{
controller.$$anonymous$$ove(Vector3.forward * speed * Time.deltaTime);
return;
}
moveVector = Vector3.zero;
if(controller.isGrounded)
{
vertivalVelocity = -0.5f;
}
else
{
vertivalVelocity -= gravity * Time.deltaTime;
}
//x - Left and Right
moveVector.x = Input.GetAxisRaw("Horizontal") * speed;
//y - Up and Down
//z - Foward and Backward
moveVector.z = speed;
controller.$$anonymous$$ove((moveVector * speed) * Time.deltaTime);
}
public void SetSpeed(float modifer)
{
speed = 2.0f * modifer;
}
//it is being called everytime our capsule hits something
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy")
Death();
}
private void Death()
{
isDead = true;
GetComponent<Score>().OnDeath ();
}