Results 1 to 5 of 5

Thread: Unity C# for noobs

  1. #1

    Lightbulb Unity C# for noobs

    If you are reading this you want to learn c#. Let's start!

    -------------------------------------------------------------------------

    Lets think we have done player movement script, and we want tell the unity how fast player walks and we want modify it in inspector

    First we need create float, float can used for speed.

    This is how is gonna looks like:

    ----------------------------------------------------------------------------------------------
    Public float Speed;
    ----------------------------------------------------------------------------------------------

    Public means that you can modify "speed" in other scripts.
    If you dont tell to unity is the component private or public unity makes it automatically private.
    ----------------------------------------------------------------------------------------------

    Lets look that code strip again:

    Public float speed
    ^
    First you need to tell is the component private or public

    --------------------------------------------------------------------

    Public float speed
    ^
    Then we need to tell what kind of component we are making.

    ---------------------------------------------------------------------
    Public float speed
    ^
    And last we name it to "speed" (you can name it whatever you want)

    ---------------------------------------------------------------------
    Now we have the speed, and it's zero because we didn't tell to unity what how fast its gonna be.
    It's easy to fix.

    -----------------------------------------------------------------------

    Public float speed = 10;

    ----------------------------------------------------------------------

    Now unity knows the speed, we can use this value in our movement

    ---------------------------------------------------------------------

    if(Input.GetKey (KeyCode.D))
    {
    transform.Translate(Vector2.right * speed * Time.deltaTime);

    }

    ---------------------------------------------------------------------

  2. #2
    Master Sorcerer MyLegGuy's Avatar
    Join Date
    Mar 2014
    Location
    United States
    Posts
    1,631

    Default

    Don't forget that public variables can be changed in the inspector. And you should also describe what a float is.

    Also this place is dead. You shouldn't bother posting here.
    http://forum.unity3d.com/

  3. #3
    Master Sorcerer |ThyLuigi|'s Avatar
    Join Date
    Dec 2014
    Location
    Alpha Centauri
    Posts
    8,191

    Default

    Code:
    Code please.
    "It vexes me how veracious discussion actually devolved into what literally is nocuous as a brainwashed drove of cattle spamming the only two words in their vocabulary: forum game." - JOSHIE63, 2017

    ↓↓↓
    IGN:TheLuigi
    ↑↑↑

  4. #4
    Master Sorcerer MyLegGuy's Avatar
    Join Date
    Mar 2014
    Location
    United States
    Posts
    1,631

    Default

    Quote Originally Posted by |ThyLuigi| View Post
    Code:
    Code please.
    It's all right there.
    Just put it together.
    I think this would work:
    Code:
    using UnityEngine;
    using System.Collections;
    
    public class Movement: MonoBehaviour {
    Public float speed = 10.5;    
        void Update() {
            if(Input.GetKey (KeyCode.D))
           {
           transform.Translate(Vector2.right * speed * Time.deltaTime);
    
          }
        }
    }

  5. #5

    Default

    Quote Originally Posted by MyLegGuy View Post
    It's all right there.
    Just put it together.
    I think this would work:
    Code:
    using UnityEngine;
    using System.Collections;
    
    public class Movement: MonoBehaviour {
    Public float speed = 10.5;    
        void Update() {
            if(Input.GetKey (KeyCode.D))
           {
           transform.Translate(Vector2.right * speed * Time.deltaTime);
    
          }
        }
    }
    It does but it moves you only right, do same with keycode.w

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •