Search Unity

Trying to understand C# scripting..

Discussion in 'Scripting' started by Kulade5, Jan 8, 2014.

  1. Kulade5

    Kulade5

    Joined:
    Jan 6, 2014
    Posts:
    4
    OK, so I'm trying to make a very basic platforming game to get myself accustomed to Unity. Right now, I'm trying to understand how the C# scripting actually works.

    Right now, I'm trying to make my character land on a platform. And I don't have much but the default script right now:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Shantae : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update ()
    15. {
    16.    
    17.     }
    18. }
    19.  
    20.  
    If it follows with just about everything else I've understood about platformers, I just have to detect the collision and move my character (Shantae) to the top pixel of the platform minus 1. To do that, I just get the position of the platform and reset Shantae's y position.

    From what I understand about Unity scripting so far, I would be doing this change in the Update() function.
    I can pretty much look at documentation for how to do all of that, but I still wonder:

    1) Is it possible to change object positions in a script like this? or is that something I have to do in the editor?
    2) I'm familiar with object-oriented programming, but I'm not sure how exactly I would get the position of a platform or any object. Surely every object has that bit of info, but how do I use it?

    I'm still just trying to understand how everything works together. By the way, both objects have a Box2D collider associated with them.

    Thanks for any help, guys.
     
  2. Deleted User

    Deleted User

    Guest

  3. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    Hey Kulade5,

    I've been programming for 16 years, and for the past 7 i've been working with C#, and Unity and C# got me a bit.

    Anywho,

    The Update portion is called every frame. This is where you could put your if's for your inputs..

    Update: if (Input.GetKeyDown(KeyCode.S)) type stuff.

    Now, i'll mention, that if you're wanting to move something, you'll want to add in the Time.deltaTime portion, otherwise movement speed will vary between systems.

    As far as getting the position of a platform, there are multiple ways of doing it. If the platform is static/not moving, you could just call a reference to it then get its transform.position, but if its not moving, i'd still store its reference, and use transform.position.

    Or you could always Raycast to see what platforms are closest.

    Just like with any form of programming, there are about 15 different ways to get the same desired results. Some, more efficient than others, but it varies per situation.

    Good luck to you.