유니티 심화

Ref

다모아 2023. 8. 18. 15:12
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RefMain : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int age = 10;
        this.SayHello(age);
        Debug.Log(age);
    }

    void SayHello(int num)
    {
        num = num + 40;
    }

}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RefMain : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int age = 10;
        this.SayHello(ref age);
        Debug.Log(age);
    }

    void SayHello(ref int num)
    {
        num = num + 40;
    }

}

 

Ref를 사용하면 원본이 변한다.

 

변수 지정을 안하면 오류가 남

 


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RefMain : MonoBehaviour
{
    [SerializeField]
    private Transform startTrans;
    [SerializeField]
    private Transform endTrans;
    [SerializeField]
    private Transform playerTrans;
    [SerializeField]
    private float smoothTime = 1f;
    [SerializeField]
    private Transform offsetTrans;

    private Vector3 velocity = Vector3.zero;

    private void Update()
    {
        //반환값 : 보간된 위치
        //this.playerTrans.position = Vector3.SmoothDamp(this.playerTrans.position, this.endTrans.position, ref velocity, this.smoothTime);
        //Debug.Log(this.velocity);

    }

    private void OnDrawGizmos()
    {
        DrawArrow.ForGizmo(this.playerTrans.position, this.playerTrans.up);
    }
}

 

 

방향벡터는 크기와 방향만 가지고있어서 위치는 중요하지않다.

위치벡터는 위치만 가지고 있으면 된다.