Skip to content

Instantly share code, notes, and snippets.

@SamatKadyrov
Last active December 5, 2023 18:28
Show Gist options
  • Save SamatKadyrov/683b4073083243fc2f36a0e9735e0e7a to your computer and use it in GitHub Desktop.
Save SamatKadyrov/683b4073083243fc2f36a0e9735e0e7a to your computer and use it in GitHub Desktop.
Mathf Methods
using UnityEngine;
public class MathfAbsExample : MonoBehaviour
{
[SerializeField] private int _number = -5;
private void Start()
{
Debug.Log(Mathf.Abs(_number)); // выведет: 5
}
}
using UnityEngine;
public class MathfClampExample : MonoBehaviour
{
[SerializeField] private float _value = 1.5f;
private float _clampedValue;
private void Start()
{
_clampedValue = Mathf.Clamp01(_value);
Debug.Log(_clampedValue); // Выведет 1
}
}
public class MathfClampExample : MonoBehaviour
{
[SerializeField] private float _value = 30;
[SerializeField] private float _minValue = 0;
[SerializeField] private float _maxValue = 10;
private float _clampedValue;
private void Start()
{
_clampedValue = Mathf.Clamp(_value, _minValue, _maxValue);
Debug.Log(_clampedValue); // Выведет 10
}
}
using UnityEngine;
public class MathfCosExample : MonoBehaviour
{
[SerializeField] private float _angle = Mathf.PI; // Угол в радианах (180 градусов)
private float _cosValue;
private void Start()
{
_cosValue = Mathf.Cos(_angle);
Debug.Log(_cosValue); // выведет: -1
}
}
using UnityEngine;
public class MathfDeg2RadExample : MonoBehaviour
{
[SerializeField] private float _angleInDegrees = 45f;
private float _angleInRadians;
// Start is called before the first frame update
private void Start()
{
_angleInRadians = _angleInDegrees * Mathf.Deg2Rad;
Debug.Log(_angleInRadians); // выведет: 0.7853982
}
}
public class MathfLerpExample : MonoBehaviour
{
[SerializeField] private float _startValue = 0;
[SerializeField] private float _endValue = 10;
[SerializeField] private float _stepCoefficient = 0.5f;
private float _interpolatedValue;
private void Start()
{
_interpolatedValue = Mathf.Lerp(_startValue, _endValue, _stepCoefficient);
Debug.Log(_interpolatedValue); // Выведет 5
}
}
using UnityEngine;
public class MathfLerpUpdateExample : MonoBehaviour
{
[SerializeField] private float _startValue = 0;
[SerializeField] private float _endValue = 10;
[SerializeField] private float _stepCoefficient = 0.5f;
private void Update()
{
_startValue = Mathf.Lerp(_startValue, _endValue, _stepCoefficient);
Debug.Log(_startValue);
}
}
using UnityEngine;
public class MathfMoveTowardsExample : MonoBehaviour
{
[SerializeField] private float _currentValue = 0;
[SerializeField] private float _endValue = 10;
[SerializeField] private float _maxMove = 2.0f;
private void Start()
{
_currentValue = Mathf.MoveTowards(_currentValue, _endValue, _maxMove);
Debug.Log(_currentValue); // Выведет 2
}
}
using UnityEngine;
public class MathfMoveTowardsUpdateExample.cs : MonoBehaviour
{
[SerializeField] private float _currentValue = 0;
[SerializeField] private float _endValue = 10;
[SerializeField] private float _maxMove = 3.0f;
private void Update()
{
_currentValue = Mathf.MoveTowards(_currentValue, _endValue, _maxMove);
Debug.Log(_currentValue);
}
}
using UnityEngine;
public class MathfRad2DegExample : MonoBehaviour
{
[SerializeField] private float _angleInRadians = 0.7853982f;
private float _angleInDegrees;
// Start is called before the first frame update
private void Start()
{
_angleInDegrees = _angleInRadians * Mathf.Rad2Deg;
Debug.Log(_angleInDegrees); // выведет: 45
}
}
using UnityEngine;
public class MathfSignExample : MonoBehaviour
{
[SerializeField] private float _number = -10f;
private float _sign;
private void Start()
{
_sign = Mathf.Sign(_number);
Debug.Log(_sign); //выведет -1
}
}
using UnityEngine;
public class MathfSinExample : MonoBehaviour
{
[SerializeField] private float _angle = Mathf.PI / 2; // угол в радианах (90 градусов)
private float _sinValue;
private void Start()
{
_sinValue = Mathf.Sin(_angle);
Debug.Log(_sinValue); // выведет: 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment