C# Basic

C# Basic

C

Object-oriented Language

다른 객체 지향 언어와 마찬가지로 Class와 Instance Attribute, Method 등의 개념이 존재한다.

C# Script 생성시 초기 화면

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}
}
  • MonoBehaviour : Unity의 기본 Class 😪 docs.unity
  • Update method : frame마다 매번 실행됨

Data Type

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{
int iValue;
double dValue;
float fValue;
bool bValue;
string sValue;

// Start is called before the first frame update
void Start()
{
iValue = 50;
dValue = 100.123;
fValue = 100.23f;
bValue = true;
sValue = "Hello World";
}

// Update is called once per frame
void Update()
{
print("Integer Value: " + iValue);
print("Double Value: " + dValue);
print("Float Value: " + fValue);
print("Boolean Value: " + bValue);
print("String Value: " + sValue);
}
}
  • float type은 number + f로 써야함

Moving Object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{
float speed = 20.0f;
Rigidbody rb;
public bool isGrounded;

// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}

void OnCollisionStay()
{
isGrounded = true;
}

// Update is called once per frame
void Update()
{
float position1 = Input.GetAxis("Vertical");
float position2 = Input.GetAxis("Horizontal");

position1 = position1 * speed * Time.deltaTime;
position2 = position2 * speed * Time.deltaTime;

transform.Translate(Vector3.forward * position1);
transform.Translate(Vector3.right * position2);

if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
isGrounded = false;
}
}
}

어설프게 키보드로 움직이는 코드

# C#, Unity
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×