Search Unity

Minimum script needed to make character move left/right

Discussion in 'Editor & General Support' started by zachn, Dec 17, 2011.

  1. zachn

    zachn

    Joined:
    Dec 17, 2011
    Posts:
    3
    Hello:

    I'm new to unity and am trying to learn scripting. I thought it would be easier to just see what makes one action tick rather than looking at the whole controller code for the 2D tutorial. So, I just want to know what the bare minimum script necessary is - just walking, no running, etc. Thanks!
     
  2. BlackSpider

    BlackSpider

    Joined:
    Oct 8, 2011
    Posts:
    48
    I think if you are a beginner you can start with one line of code.

    Add this line of code to the update function of your script.

    Code (csharp):
    1. this.transform.Translate(Input.GetAxis("Horizontal"),0,0);
    this moves the character on the X as with the Arrow keys or A and D.
    Here you can add a speed Variable and the time.deltatime Variable.
    Also you can start an animation when you do this.
     
    Last edited: Dec 17, 2011
  3. zachn

    zachn

    Joined:
    Dec 17, 2011
    Posts:
    3
    I don't need the "var canControl = true;"?

    as in "this".transform... you mean "Character" or whatever object is is receiving the input?

    What is the difference between that line and this line: var h = Input.GetAxisRaw ("Horizontal");

    I understand one declares and defines "h" but it uses GetAxisRaw and without the ,0,0.
     
  4. BlackSpider

    BlackSpider

    Joined:
    Oct 8, 2011
    Posts:
    48
    Do you mean this.
    Code (csharp):
    1.  
    2. if(canControl == true)
    3. {
    4. float h = Input.GetAxis("Horizontal");
    5.        
    6. this.transform.Translate(h,0,0);
    7. }
    This is the same as what i did only you use a temp variable to store the input.

    And the Raw and non Raw difference is that Raw on the keyboard only can be -1,0 or 1.

    Also the canControl bool is usefull for controlling if the player may move or not.
     
    Last edited: Dec 17, 2011
    undevable likes this.
  5. zachn

    zachn

    Joined:
    Dec 17, 2011
    Posts:
    3
    Ok, so Input.GetAxis might be used for an analog rather than the arrows or a d-pad. It can pick-up all the inbetween...

    canControl isn't necessary then unless at some point in the game the character enters into some vehicle or whatever?

    What about where you are saying "this"? what is "this"? Is that whatever object you are assigning the attribute to?
     
  6. BlackSpider

    BlackSpider

    Joined:
    Oct 8, 2011
    Posts:
    48
    Yes the Input.GetAxis is for more smooth gameplay. Also GetAxisRaw can be 0,5 but only with an anolog stick or steering wheel.

    Also you can slow down the player with a Speed variable. So you can control his speed even more. It is also very important to use Time.deltaTime. This will move the player per Seconds and not per Frame.

    And you are right on the "this" part. "this" stands for the GameObject where the script is attached to.
    Also it is possible to make a public GameObject and control another gameobject trough another script. Like:

    Code (csharp):
    1.  
    2. public GameObject Character;
    3. public float Speed = 5f;
    4. public bool canControl;
    5.  
    6. void Update()
    7. {
    8.     if(canControl == true)
    9.     {
    10.         float h = Input.GetAxis("Horizontal") * Speed;
    11.                
    12.         Character.transform.Translate(h*Time.deltaTime,0,0);
    13.     }
    14. }
    15.  
     
    Last edited: Dec 18, 2011
  7. megadavido

    megadavido

    Joined:
    Jan 16, 2017
    Posts:
    10
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerController2 : MonoBehaviour
    4. {
    5.     public float Speed = 5f;
    6.    
    7.     void Update()
    8.     {
    9.         float h = Input.GetAxisRaw("Horizontal");
    10.  
    11.         GetComponent<Rigidbody2D>().velocity = new Vector2(h, 0) * Speed;
    12.     }
    13. }
     
    Kaijuu123 and undevable like this.
  8. unity_EFC46AF244F037C18955

    unity_EFC46AF244F037C18955

    Joined:
    Apr 9, 2021
    Posts:
    1
    thank you this very helpful
     
  9. MacksNotCool

    MacksNotCool

    Joined:
    Apr 28, 2020
    Posts:
    7
    Unrelated but this was posted the exact day I got my first laptop.