Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Rhianasaur · May 10, 2017 at 01:40 PM · 2d game2d-platformercharacter controllercharacter movementspeed issues

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;
     }
 }
     
 }


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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
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
avatar image LexLthr · Jun 15, 2018 at 01:01 PM 0
Share

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 (); 

 }

avatar image
0

Answer by Varniukas · May 10, 2017 at 09:10 PM

alt text


move.png (13.3 kB)
Comment
Add comment · Share
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
avatar image
0

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);

Comment
Add comment · Share
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
avatar image
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;
 }
Comment
Add comment · Show 1 · Share
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
avatar image ashkanaral · May 16, 2020 at 09:25 PM 0
Share

Vector3 horizontal = new Vector3(Input.GetAxis("Horizontal") * (times Speed you choose), 0.0f, 0.0f);

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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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