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. Dismiss Notice

Question character teleports to 0,0,0 instantly. What to I do?

Discussion in 'Scripting' started by matidevelop, Jun 3, 2023.

  1. matidevelop

    matidevelop

    Joined:
    May 29, 2023
    Posts:
    2
    while trying to move (usage of arrows) character stays in one place (0,0) I have tried a lot of thing to fix it but nothing seems to work, maybe it is a problem in the code?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     Vector2 moveDirection;
    9.     [SerializeField] float moveSpeed;
    10.  
    11.     bool speedUp;
    12.     [SerializeField] float speedUpMultiplier;
    13.  
    14.  
    15.     void Start()
    16.     {
    17.      
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.    
    24.        
    25.      moveDirection.x = Input.GetAxisRaw("Horizontal");
    26.      moveDirection.y = Input.GetAxisRaw("Vertical");
    27.      speedUp = Input.GetKey(KeyCode.Z) ;        //create SpeedUp axis in input manager and bind it to L/R shift
    28.  
    29.      transform.position = Time.deltaTime * moveSpeed * (speedUp ? speedUpMultiplier : 1) * moveDirection.normalized;
    30.        
    31.  
    32.     }
    33. }
    34.  
     
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    On line 29 it looks like you're setting the position every frame. Change the = to += and you'll add some position every frame instead (I think that's what you want).

    Note you'll have to change your moveDirection to a Vector3 to avoid the ambiguity between the position Vector3 and the moveDirection Vector2.

    Seeing as you're just starting, it'd probably be easier if you made all your fields public for now. It'll be useful to see how those behave in the inspector as you press your control keys.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Not sure where you got line 29 from, but:

    - for one it is WAAAAAY too long to reason about, and
    - for two, it computes a position that is always going to be relatively close to zero.

    Instead:

    - select speed based on speedup, put that in a temp variable
    - compute a movement vector from your inputs
    - scale it by chosen speed
    - scale it by Time.deltaTime
    - put the transform position in a temp variable
    - add the position to the movement
    - set the position back to the transform.

    Your design incorporates at least those seven steps so don't try to be clever by packing them into one line, as it will not impress the computer.