Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

movement code isn't working

Discussion in 'Scripting' started by thefreshcoder21, Mar 13, 2020.

  1. thefreshcoder21

    thefreshcoder21

    Joined:
    Mar 13, 2020
    Posts:
    6
    the way the code is supposed to work is you move 1 tile at a time but for some reason you move between half of a tile or 1.2 of a tile
    here is the code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class move_v2 : MonoBehaviour
    6. {
    7.     public float moveSped = 1;
    8.     public float notmoveSped = -1;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if (Input.GetKeyDown(KeyCode.W))
    19.         {
    20.             this.transform.Translate(Vector3.forward * moveSped * Time.deltaTime);
    21.         }
    22.         if (Input.GetKeyDown(KeyCode.A))
    23.         {
    24.             this.transform.Translate(Vector3.right * notmoveSped * Time.deltaTime);
    25.         }
    26.         if (Input.GetKeyDown(KeyCode.S))
    27.         {
    28.             this.transform.Translate(Vector3.forward * notmoveSped * Time.deltaTime);
    29.         }
    30.         if (Input.GetKeyDown(KeyCode.D))
    31.         {
    32.             this.transform.Translate(Vector3.right * moveSped * Time.deltaTime);
    33.         }
    34.     }
    35. }
    36.  
     
  2. Karrzun

    Karrzun

    Joined:
    Oct 26, 2017
    Posts:
    123
    I guess you're mixing up destination and position here. You usually want to multiply by Time.deltaTime when calculating an object's current position but I think you're trying to set its final position (= destination) by a fixed offset in which case you shouldn't multiply by Time.deltaTime.

    So removing that additional factor should already solve your problem although that probably leads to (possibly unwanted?) abrupt movement.
     
  3. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    Assuming one key press should move the character one tile smoothly...

    ---

    You probably want to separate input and movement, here's some rough PSUEDO code (which only works in X) to convey the idea:

    Code (csharp):
    1.  
    2. Update() {
    3.   ProcessInput();
    4.   Move();
    5. }
    6.  
    7. ProcessInput() {
    8.   if (Input.GetKeyDown(KeyCode.D)) {
    9.     UpdatePosition(1, 0);
    10.   } else if (Input.GetKeyDown(KeyCode.A)) {
    11.     UpdatePosition(-1, 0);
    12.   }
    13. }
    14.  
    15. UpdatePosition(int x, int y) {
    16.   destinationX = currentPositionX + x;
    17.   // Limit to 1 tile at a time, remove this and pressing keys over and over will 'stack'
    18.   if (destinationX > currentPositionX + 1) destinationX = currentPositionX + 1;
    19.   if (destinationX < currentPositionX - 1) destinationX = currentPositionX - 1;
    20. }
    21.  
    22. Move() {
    23.   // If we haven't reached destination we need to move
    24.   if (currentPositionX != destinationX) {
    25.     float dir = (currentPosition < destination X) ? 1.0f : -1.0f;
    26.     transform.Translate(Vector3.right * dir * moveSped * Time.deltaTime);
    27.     // If we move too far or just far enough set exact position and update current position
    28.     if ((transform.Position.x >= destinationX && dir > 0) ||
    29.         (transform.Position.x <= destinationX && dir < 0)) {
    30.       currentPositionX = destinationX;
    31.       transform.Position.x = destinationX;
    32.   }
    33. }
    34.  
     
    Last edited: Mar 13, 2020
  4. thefreshcoder21

    thefreshcoder21

    Joined:
    Mar 13, 2020
    Posts:
    6
    thank you it worked