Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Move Left ,Right, Up, Down Script for noobs C#

Discussion in 'Scripting' started by oOoJaMz93oOo, Feb 3, 2013.

  1. oOoJaMz93oOo

    oOoJaMz93oOo

    Joined:
    Oct 2, 2012
    Posts:
    13
    Here is a script for getting a cube to move around .
    Just Apply to a cube or object of your choice



    Code (csharp):
    1. [public class CubeMove : MonoBehaviour {
    2.     public float moveSpeed = 3f;
    3.     // Use this for initialization
    4.     void Start () {
    5.    
    6.     }
    7.    
    8.     // Update is called once per frame
    9.     void Update ()
    10.     {
    11.         //Moves Forward and back along z axis                           //Up/Down
    12.     transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("Vertical")* moveSpeed);
    13.         //Moves Left and right along x Axis                               //Left/Right
    14.     transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal")* moveSpeed);      
    15.     }
    16. }/CODE]
     
    Caz2K and wolveskraft like this.
  2. Marulo008

    Marulo008

    Joined:
    Feb 20, 2015
    Posts:
    1
    How do I make the object collide with other objects while the first object is moving (with this script)?
     
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    To get collision while moving, the object has to have a CharacterController, or a Rigidbody component attached.

    Replace transform.Translate with characterController.Move, or Rigidbody.MovePosition (or set the velocity directly).

    You have to add the components to the GameObject, then store them into a variable. In Start, use characterController = GetComponent<CharacterController>();
     
  4. Aarin123

    Aarin123

    Joined:
    Feb 9, 2017
    Posts:
    1
    SDFGHJ
     
  5. Blaze283

    Blaze283

    Joined:
    Feb 12, 2017
    Posts:
    9
    How do I cancel backward movement from getAxis?
     
  6. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    Code (CSharp):
    1. if (Input.GetAxis("Name") > 0f)
    2. {
    3.     ...
    4. }
     
  7. slibiran12

    slibiran12

    Joined:
    Dec 6, 2016
    Posts:
    1
    what will im gonna code if i press two times forward key to make the movement faster?
     
  8. funnyh34

    funnyh34

    Joined:
    Apr 6, 2018
    Posts:
    1
    This is the most recent and current script for cube movement since 2018 April.

    using UnityEngine;

    [public class CubeMove : MonoBehaviour
    {
    public float moveSpeed = 3f;
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    //Moves Forward and back along z axis //Up/Down
    transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("Vertical") * moveSpeed);
    //Moves Left and right along x Axis //Left/Right
    transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
    }
    }
     
  9. vcstylez1

    vcstylez1

    Joined:
    Oct 25, 2017
    Posts:
    2
    Hi, I am trying to make a 3D bomberman style game and the above really helped with getting my character moving, however I am after restricting movement to just linear up, down, left and right with no diagonal movement possible, what would I need to add to the code above in order to achieve this?
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    For future reference, you should probably post your own thread with information (and code) about your question.

    However, one idea would be to check if the axis has any input, for instance:
    Code (csharp):
    1. float h = Input.GetAxis("Horizontal");
    2. float v = Input.GetAxis("Vertical");
    3.  
    4. if(h != 0) {
    5.    // horizontal
    6.  }
    7.  // with 'else if', vertical changes can only occur if there is no horizontal. You could use this order or flip them
    8. // either way, I think this is what you were asking about.
    9. else if (v != 0) {
    10.  }
     
  11. rohitmohanraj001

    rohitmohanraj001

    Joined:
    Aug 25, 2018
    Posts:
    1
    hey I NEED CODINGS FOR MAKE MY PLAYER TO MOVE LIKE IN GTA
     
  12. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Click on "Learn" at the top of this page - and go easy on the cap's. :)
     
    EToSVn likes this.
  13. adawiyahabdjalil

    adawiyahabdjalil

    Joined:
    Mar 1, 2019
    Posts:
    1


    I cannot move the up and down movement. Can anyone help me please?
     
  14. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    Same as above except instead of Vector3.forward or Vector3.right, use Vector3.up
     
  15. matyosjoseph930

    matyosjoseph930

    Joined:
    Feb 27, 2019
    Posts:
    1
    does not work
     
  16. Boldlefty

    Boldlefty

    Joined:
    Jun 1, 2019
    Posts:
    1
    replace Vector3.forward with Vector3.up
     
  17. Tom_Madden

    Tom_Madden

    Joined:
    Nov 10, 2016
    Posts:
    1
    When i use this i get 5 errors, Identifier expected, 2 end of file expected, ']' expected and cannot directly contain members such as fields or methods. Anyone know why?
    Edit: I got rid of 3 errors by deleting the {/CODE] thing at the end. i underlined them. i also got a new one '}' expected.
     
    Last edited: Jun 18, 2019
  18. Batuhan13

    Batuhan13

    Joined:
    Apr 9, 2017
    Posts:
    117
    Hi mate can you share your code with us?There are a few reasons to cause these errors and proably one of them is your problem.First of all you need to check your "{","}" symbols on your code.Their counts must be equal.Secondly you need to sure about name of your code I mean in the begining of your code there is a line start like this "publis class ""your script name "" : MonoBehavior in here your script name and c# file name in your project assets must be equal.If they are correct proably you will have no problem :) Good Luck
     
  19. rakeshbhure

    rakeshbhure

    Joined:
    Sep 14, 2019
    Posts:
    2
    Code (CSharp):
    1. flote h = Inpute.GetAxis ("horizontal");
    2. flote v = Inpute.GetAxis ("verticale");
    3. If (h ! = 0 )
    4.  
    5. {Horizontal}
    6.  
    7. else if  (v!=0)
    8. {Verticale}
     
  20. rakeshbhure

    rakeshbhure

    Joined:
    Sep 14, 2019
    Posts:
    2
    Code (CSharp):
    1. transform.translet (vector 3.forword*time.deltatime * Inpute.GateAxis("verticale")* movespeed);
     
  21. AndrewThedumb

    AndrewThedumb

    Joined:
    Jul 5, 2020
    Posts:
    1
    how the hell do you do this S*** its currently 3:24 in the damn morning and i've been coding for 17 hours trying to make my person move and NOTHING WORKS i want to die
     
  22. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    Batuhan13 likes this.
  23. Batuhan13

    Batuhan13

    Joined:
    Apr 9, 2017
    Posts:
    117
    Some developer problems :D
     
  24. Caz2K

    Caz2K

    Joined:
    Jul 30, 2020
    Posts:
    1
    worked
     
  25. TheRealFowluhh

    TheRealFowluhh

    Joined:
    Nov 27, 2021
    Posts:
    1
    Can You Give Me The Full Thing Because It Is Not Working For Me

    Thank You