- Home /
How to change the movement speed of a character
I'm creating a 2D platformer game and have managed to a get a working script to move the character using the arrow keys, however it moves very quickly. I tried introducing a speed variable into the script but that broke the movement all together :(
Any ideas on how to implement a speed? Script below
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour {
 // Use this for initialization
 void Start () {
     
 }
 public float speed = 5f;
 void Update () {
     if (Input.GetKey(KeyCode.LeftArrow))
     {
         Vector3 position = this.transform.position;
         position.x--;
         this.transform.position = position * speed;
     }
     if (Input.GetKey(KeyCode.RightArrow))
     {
         Vector3 position = this.transform.position;
         position.x++;
         this.transform.position = position * speed;
     }
 }
     
 }
Answer by Patrick2607 · May 10, 2017 at 01:47 PM
You don't take the time it needs to render a frame into account. I think this is what you need:
 void Update () {
     if (Input.GetKey(KeyCode.LeftArrow))
     {
         this.transform.position.x -= speed * Time.deltaTime;
     }
     if (Input.GetKey(KeyCode.RightArrow))
     {
         this.transform.position.x += speed * Time.deltaTime;
     }
 }
You can adjust the speed accordingly.
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 (); 
 }
Answer by William4458 · Jun 15, 2018 at 01:08 PM
Use InputAxis...
you can compress everything into one line:
transform.Translate (Input.GetAxis ("Horizontal") speed Time.deltaTime, 0, 0);
Answer by BruteAsura · Apr 13, 2020 at 06:21 PM
I need to state the speed of this script, can someone please help me? Script:
public class BasicMovement : MonoBehaviour { // Start is called before the first frame update void Start() {
 }
 // Update is called once per frame
 void Update()
 {
     Vector3 horizontal = new Vector3(Input.GetAxis("Horizontal"), 0.0f, 0.0f);
     transform.position = transform.position + horizontal * Time.deltaTime;
 }
Vector3 horizontal = new Vector3(Input.GetAxis("Horizontal") * (times Speed you choose), 0.0f, 0.0f);
Your answer
 
 
             Follow this Question
Related Questions
[SOLVED] 2D Character Controller gains velocity when colliding with a corner 1 Answer
How to get smooth 4 directional movement in top down without diagonal movement? 2 Answers
Delayed response: Jumping up from ledge 0 Answers
Poor quality sprite 2D game 3 Answers
How can i use single swipe to run a character continuously. 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                
