برخی از متدهای کلاسهای
### Vector2:
1. `Distance(Vector2 a, Vector2 b)`:
- توضیح: این متد فاصلهی اقلیدسی (Euclidean distance) بین دو نقطهی مشخص را محاسبه میکند.
- نمونه کد:
2. `Dot(Vector2 lhs, Vector2 rhs)`:
- توضیح: این متد محصول نقطهای (dot product) دو بردار را محاسبه میکند.
- نمونه کد:
### Vector3:
1. `Angle(Vector3 from, Vector3 to)`:
- توضیح: این متد زاویهی بین دو بردار را با استفاده از محصول نقطهای محاسبه میکند.
- نمونه کد:
2. `Reflect(Vector3 inDirection, Vector3 inNormal)`:
- توضیح: این متد بازتاب بردار وارون شده از یک سطح را محاسبه میکند.
- نمونه کد:
3. `Project(Vector3 vector, Vector3 onNormal)`:
- توضیح: این متد پروژکشن یک بردار بر روی یک بردار دیگر را محاسبه میکند.
- نمونه کد:
4. `Lerp(Vector3 a, Vector3 b, float t)`:
- توضیح: این متد یک بردار در طول یک مسیر خطی بین دو بردار مبدأ و مقصد ایجاد میکند.
- نمونه کد:
5. `Slerp(Vector3 a, Vector3 b, float t
)`:
- توضیح: این متد یک بردار در طول یک مسیر قطبی بین دو بردار مبدأ و مقصد ایجاد میکند.
- نمونه کد:
6. `RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta)`:
- توضیح: این متد یک بردار را به سمت یک بردار هدف چرخانده و محدودیتهایی برای حرکت را اعمال میکند.
- نمونه کد:
7. `MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)`:
- توضیح: این متد یک بردار را به سمت یک بردار هدف حرکت میدهد با محدودیت فاصله حداکثری.
- نمونه کد:
8. `SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed)`:
- توضیح: این متد یک بردار را به سمت یک بردار هدف با استفاده از حرکت انسیابی (Smooth Damping) حرکت میدهد.
- نمونه کد:
#یونیتی #برنامه_نویسی
Vector2 و Vector3 همراه با نمونه کد:### Vector2:
1. `Distance(Vector2 a, Vector2 b)`:
- توضیح: این متد فاصلهی اقلیدسی (Euclidean distance) بین دو نقطهی مشخص را محاسبه میکند.
- نمونه کد:
Vector2 pointA = new Vector2(1.0f, 2.0f);
Vector2 pointB = new Vector2(4.0f, 6.0f);
float distance = Vector2.Distance(pointA, pointB);
2. `Dot(Vector2 lhs, Vector2 rhs)`:
- توضیح: این متد محصول نقطهای (dot product) دو بردار را محاسبه میکند.
- نمونه کد:
Vector2 vectorA = new Vector2(2.0f, 3.0f);
Vector2 vectorB = new Vector2(1.0f, 2.0f);
float dotProduct = Vector2.Dot(vectorA, vectorB);
### Vector3:
1. `Angle(Vector3 from, Vector3 to)`:
- توضیح: این متد زاویهی بین دو بردار را با استفاده از محصول نقطهای محاسبه میکند.
- نمونه کد:
Vector3 fromVector = new Vector3(1.0f, 0.0f, 0.0f);
Vector3 toVector = new Vector3(0.0f, 1.0f, 0.0f);
float angle = Vector3.Angle(fromVector, toVector);
2. `Reflect(Vector3 inDirection, Vector3 inNormal)`:
- توضیح: این متد بازتاب بردار وارون شده از یک سطح را محاسبه میکند.
- نمونه کد:
Vector3 incident = new Vector3(1.0f, -1.0f, 0.0f).normalized;
Vector3 normal = new Vector3(0.0f, 1.0f, 0.0f).normalized;
Vector3 reflected = Vector3.Reflect(incident, normal);
3. `Project(Vector3 vector, Vector3 onNormal)`:
- توضیح: این متد پروژکشن یک بردار بر روی یک بردار دیگر را محاسبه میکند.
- نمونه کد:
Vector3 vectorToProject = new Vector3(3.0f, 4.0f, 5.0f);
Vector3 ontoVector = new Vector3(1.0f, 0.0f, 0.0f);
Vector3 projectedVector = Vector3.Project(vectorToProject, ontoVector);
4. `Lerp(Vector3 a, Vector3 b, float t)`:
- توضیح: این متد یک بردار در طول یک مسیر خطی بین دو بردار مبدأ و مقصد ایجاد میکند.
- نمونه کد:
Vector3 vectorA = new Vector3(1.0f, 2.0f, 3.0f);
Vector3 vectorB = new Vector3(4.0f, 5.0f, 6.0f);
Vector3 lerpedVector = Vector3.Lerp(vectorA, vectorB, 0.5f);
5. `Slerp(Vector3 a, Vector3 b, float t
)`:
- توضیح: این متد یک بردار در طول یک مسیر قطبی بین دو بردار مبدأ و مقصد ایجاد میکند.
- نمونه کد:
Vector3 vectorA = new Vector3(1.0f, 0.0f, 0.0f);
Vector3 vectorB = new Vector3(0.0f, 1.0f, 0.0f);
Vector3 slerpedVector = Vector3.Slerp(vectorA, vectorB, 0.5f);
6. `RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta)`:
- توضیح: این متد یک بردار را به سمت یک بردار هدف چرخانده و محدودیتهایی برای حرکت را اعمال میکند.
- نمونه کد:
Vector3 currentVector = new Vector3(1.0f, 0.0f, 0.0f);
Vector3 targetVector = new Vector3(0.0f, 1.0f, 0.0f);
float maxRadiansDelta = 0.1f;
float maxMagnitudeDelta = 0.01f;
Vector3 rotatedVector = Vector3.RotateTowards(currentVector, targetVector, maxRadiansDelta, maxMagnitudeDelta);
7. `MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)`:
- توضیح: این متد یک بردار را به سمت یک بردار هدف حرکت میدهد با محدودیت فاصله حداکثری.
- نمونه کد:
Vector3 currentVector = new Vector3(1.0f, 0.0f, 0.0f);
Vector3 targetVector = new Vector3(0.0f, 1.0f, 0.0f);
float maxDistanceDelta = 0.1f;
Vector3 movedVector = Vector3.MoveTowards(currentVector, targetVector, maxDistanceDelta);
8. `SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed)`:
- توضیح: این متد یک بردار را به سمت یک بردار هدف با استفاده از حرکت انسیابی (Smooth Damping) حرکت میدهد.
- نمونه کد:
Vector3 currentVector = new Vector3(1.0f, 0.0f, 0.0f);
Vector3 targetVector = new Vector3(0.0f, 1.0f, 0.0f);
Vector3 currentVelocity = Vector3.zero;
float smoothTime = 0.5f;
float maxSpeed = 2.0f;
Vector3 smoothedVector = Vector3.SmoothDamp(currentVector, targetVector, ref currentVelocity, smoothTime, maxSpeed);
#یونیتی #برنامه_نویسی
🔥5
در Unity، کلاس
1. `Abs(float value)`:
- توضیح: بازگرداندن مقدار مطلق یک عدد.
- نمونه کد:
2. `Sin(float f)`, `Cos(float f)`, `Tan(float f)`:
- توضیح: محاسبه سینوس، کسینوس و تانژانت یک زاویه به رادیان.
- نمونه کد:
3. `Sqrt(float f)`:
- توضیح: محاسبه ریشه مربعی یک عدد.
- نمونه کد:
4. `Pow(float f, float p)`:
- توضیح: محاسبه توان یک عدد به توان دیگر.
- نمونه کد:
5. `Round(float f)`:
- توضیح: گرد کردن یک عدد به نزدیکترین عدد صحیح.
- نمونه کد:
6. `Ceil(float f)`, `Floor(float f)`:
- توضیح: گرد کردن به سمت بالا و پایین به نزدیکترین عدد صحیح.
- نمونه کد:
7. `Min(float a, float b)`, `Max(float a, float b)`:
- توضیح: بازگرداندن کمترین یا بزرگترین عدد از دو عدد داده شده.
- نمونه کد:
8. `Clamp(float value, float min, float max)`:
- توضیح: محدود کردن یک عدد بین یک حداقل و حداکثر مشخص.
- نمونه کد:
9. `Lerp(float a, float b, float t)`:
- توضیح: ایجاد یک عدد در طول یک مسیر خطی بین دو عدد مبدأ و مقصد.
- نمونه کد:
10. `InverseLerp(float a, float b, float value)`:
- توضیح: محاسبه مقدار پارامتر t که عدد value بین a و b قرار دارد.
- نمونه کد:
11. `MoveTowards(float current, float target, float maxDelta)`:
- توضیح: حرکت یک عدد به سمت یک عدد هدف با محدودیت فاصله حداکثری.
- نمونه کد:
12. `SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed)`:
- توضیح: انجام حرکت انسیابی (Smooth Damping) عدد به سمت یک عدد هدف.
- نمونه کد:
#یونیتی #برنامه_نویسی
Mathf حاوی متدها و توابع ریاضی مختلفی است که برای انجام عملیات ریاضی مورد نیاز در بازیها و شبیهسازیهای سهبعدی استفاده میشوند. در زیر توضیحات برخی از متدهای این کلاس به همراه نمونه کد آمده است:1. `Abs(float value)`:
- توضیح: بازگرداندن مقدار مطلق یک عدد.
- نمونه کد:
float absoluteValue = Mathf.Abs(-5.0f);
2. `Sin(float f)`, `Cos(float f)`, `Tan(float f)`:
- توضیح: محاسبه سینوس، کسینوس و تانژانت یک زاویه به رادیان.
- نمونه کد:
float angleInRadians = Mathf.PI / 4.0f; // 45 degrees in radians
float sineValue = Mathf.Sin(angleInRadians);
float cosineValue = Mathf.Cos(angleInRadians);
float tangentValue = Mathf.Tan(angleInRadians);
3. `Sqrt(float f)`:
- توضیح: محاسبه ریشه مربعی یک عدد.
- نمونه کد:
float squareRootValue = Mathf.Sqrt(25.0f); // 5.0
4. `Pow(float f, float p)`:
- توضیح: محاسبه توان یک عدد به توان دیگر.
- نمونه کد:
float result = Mathf.Pow(2.0f, 3.0f); // 8.0
5. `Round(float f)`:
- توضیح: گرد کردن یک عدد به نزدیکترین عدد صحیح.
- نمونه کد:
float roundedValue = Mathf.Round(3.7f); // 4.0
6. `Ceil(float f)`, `Floor(float f)`:
- توضیح: گرد کردن به سمت بالا و پایین به نزدیکترین عدد صحیح.
- نمونه کد:
float ceilValue = Mathf.Ceil(3.2f); // 4.0
float floorValue = Mathf.Floor(3.8f); // 3.0
7. `Min(float a, float b)`, `Max(float a, float b)`:
- توضیح: بازگرداندن کمترین یا بزرگترین عدد از دو عدد داده شده.
- نمونه کد:
float minValue = Mathf.Min(5.0f, 7.0f); // 5.0
float maxValue = Mathf.Max(3.0f, 9.0f); // 9.0
8. `Clamp(float value, float min, float max)`:
- توضیح: محدود کردن یک عدد بین یک حداقل و حداکثر مشخص.
- نمونه کد:
float clampedValue = Mathf.Clamp(3.0f, 1.0f, 5.0f); // 3.0
9. `Lerp(float a, float b, float t)`:
- توضیح: ایجاد یک عدد در طول یک مسیر خطی بین دو عدد مبدأ و مقصد.
- نمونه کد:
float lerpedValue = Mathf.Lerp(1.0f, 3.0f, 0.5f); // 2.0
10. `InverseLerp(float a, float b, float value)`:
- توضیح: محاسبه مقدار پارامتر t که عدد value بین a و b قرار دارد.
- نمونه کد:
float inverseLerpValue = Mathf.InverseLerp(1.0f, 5.0f, 3.0f); // 0.5
11. `MoveTowards(float current, float target, float maxDelta)`:
- توضیح: حرکت یک عدد به سمت یک عدد هدف با محدودیت فاصله حداکثری.
- نمونه کد:
float current = 2.0f;
float target = 5.0f;
float maxDelta = 1.0f;
float movedValue = Mathf.MoveTowards(current, target, maxDelta); // 3.0
12. `SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed)`:
- توضیح: انجام حرکت انسیابی (Smooth Damping) عدد به سمت یک عدد هدف.
- نمونه کد:
float current = 2.0f;
float target = 5.0f;
float currentVelocity = 0.0f;
float smoothTime = 0.5f;
float maxSpeed = 2.0f;
float smoothedValue = Mathf.SmoothDamp(current, target, ref currentVelocity, smoothTime, max
Speed);
#یونیتی #برنامه_نویسی
🔥5👍2
