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

2D top-down Game

Discussion in 'Getting Started' started by TheSaltySeaFood, Apr 7, 2020.

  1. TheSaltySeaFood

    TheSaltySeaFood

    Joined:
    Mar 19, 2020
    Posts:
    4
    My code is meant to move the sprite up the y axis and across the x axis, it moves the sprite across the x axis fine but when I try and make it move up it changes the Z coordinate.
    Here's my code please help.
    Code (CSharp):
    1.  
    2.  
    3.  
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using UnityEngine;
    7.  
    8.  
    9. public class player_movement : MonoBehaviour
    10.     {
    11.  
    12.  
    13.  
    14.     Vector2 movement;
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         float moveSpeed = 10;
    19.         //Define the speed at which the object moves.
    20.  
    21.         movement.x = Input.GetAxis("Horizontal");
    22.  
    23.         //Get the value of the Horizontal input axis.
    24.  
    25.  
    26.         movement.y = Input.GetAxis("Vertical");
    27.         //Get the value of the Vertical input axis.
    28.  
    29.         transform.Translate(new Vector3(movement.x, 0, movement.y) * moveSpeed * Time.deltaTime);
    30.         //Move the object to XYZ coordinates defined as movemenet.x, 0, and movement.y respectively
    31.     }
    32.  
    33.  
    34.  
    35. }
    36.  
    Thank you
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,851
    You wrote: "new Vector3(movement.x, 0, movement.y)". The parameters to the Vector3 constructor are x, y, and z. The y parameter here is 0, and the z parameter is movement.y. If that's not what you want, then provide them in the other order.
     
  3. TheSaltySeaFood

    TheSaltySeaFood

    Joined:
    Mar 19, 2020
    Posts:
    4
    Thanks!
     
    JoeStrout likes this.
  4. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    19
    Also, is there a reason you want to use a Vector3 instead of a Vector2?