- Home /
How can I get my Player (which is a square) to move smoothly in Unity 2d? I still need help, even though there are answers.
I can't get my square to move. I want to be able to use the A and D keys to move my player (the square). This is what I currently have: Edit: This is what I now have, and I can move my player, but i can only move it a little bit per keypress. Is there a way i can make it so that I constantly move, for as long as A or D is held down?
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Sprites;
 
 public class PlayerController : MonoBehaviour 
 {
     public float speed;
 
     private Rigidbody2D rb;
 
     void Update () {
 
         if (Input.GetKeyDown (KeyCode.A)) 
     {
         transform.Translate (-Vector3.right * Time.deltaTime * speed);
     }
     if (Input.GetKeyDown (KeyCode.D))
     {
         transform.Translate (-Vector3.right * Time.deltaTime * speed);
     }
     }
 }
Answer by Tona999 · Jun 21, 2017 at 06:25 PM
Use this: https://docs.unity3d.com/ScriptReference/Transform.Translate.html
I guess this is how it is supposed to work.
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.Sprites;
  
  public class PlayerController : MonoBehaviour 
  {
      public float speed;
  
      void Update () {
  
          if (Input.GetKeyDown (KeyCode.A)) 
          {
              transform.Translate(-Vector3.right*Time.deltaTime *speed);
          }
  
          if (Input.GetKeyDown (KeyCode.D))
          {
             transform.Translate(Vector3.right*Time.deltaTime *speed);
          }
      }
  }
Answer by Linkthehylian04 · Jun 21, 2017 at 06:57 PM
@Tona999 Now I can move my sprite, but it isn't very consistent. Is there a way that if i hold down A or D, my sprite will keep moving?
Your answer
 
 
             Follow this Question
Related Questions
Getting the Player in a Script! 2 Answers
Unity Camera and transform? 1 Answer
Why do the player's health points go down so fast? 1 Answer
Transform player from one position to another after completing a goal 1 Answer
Distribute terrain in zones 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                