KEMBAR78
Object Pooling-Hashmap-Lerps | PDF | C Sharp (Programming Language) | Computer Programming
0% found this document useful (0 votes)
25 views3 pages

Object Pooling-Hashmap-Lerps

The document provides a comprehensive overview of various types of lerps (linear interpolation) in Unity, including Linear Lerp, Spherical Lerp, SmoothStep Lerp, LerpAngle, LerpUnclamped, InverseLerp, and LerpPrecise. Each type is explained with practical C# script examples demonstrating their usage in Unity for smooth transitions and animations. The article emphasizes the importance of understanding these techniques for enhancing game visuals and user experience.

Uploaded by

ravian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Object Pooling-Hashmap-Lerps

The document provides a comprehensive overview of various types of lerps (linear interpolation) in Unity, including Linear Lerp, Spherical Lerp, SmoothStep Lerp, LerpAngle, LerpUnclamped, InverseLerp, and LerpPrecise. Each type is explained with practical C# script examples demonstrating their usage in Unity for smooth transitions and animations. The article emphasizes the importance of understanding these techniques for enhancing game visuals and user experience.

Uploaded by

ravian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Shared Chat • Default

C# Unity Object Pooling


September 17, 2023

write a simple c# unity 3d script for object pooling

Sure! Here's a simple C# script for object pooling in Unity:


csharp Copy code

using System.Collections.Generic;
using UnityEngine;

public class ObjectPooler : MonoBehaviour


{
public GameObject objectToPool;
public int poolSize = 10;
public bool canExpand = true;

private List<GameObject> pooledObjects = new List<GameObject>();

void Start()
{
for (int i = 0; i < poolSize; i++)
{
GameObject obj = Instantiate(objectToPool);
obj.SetActive(false);
pooledObjects.Add(obj);
}
}

public GameObject GetPooledObject()


{
for (int i = 0; i < pooledObjects.Count; i++)
{
if (!pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}

if (canExpand)
{
GameObject obj = Instantiate(objectToPool);
obj.SetActive(false);
pooledObjects.Add(obj);
return obj;
}

return null;
}

public void ReturnPooledObject(GameObject obj)


{
obj.SetActive(false);
}
}

To use this script, create an empty GameObject in your scene and attach this script to it.
Then, in the Unity editor, you'll be able to assign the object you want to pool (e.g., a bullet
prefab) to the objectToPool variable. Set the poolSize to the number of objects you
want to initially pool.
In your game code, you can now call GetPooledObject() to retrieve an inactive object
from the pool, and ReturnPooledObject(obj) to return it to the pool when you're done
using it.
For example:
csharp Copy code

ObjectPooler objectPooler;

void Start()
{
objectPooler = GetComponent<ObjectPooler>();
GameObject obj = objectPooler.GetPooledObject();
// Set position, rotation, etc. of the object as needed.
obj.SetActive(true);
// ...
// When you're done with the object:
objectPooler.ReturnPooledObject(obj);
}

This is a basic implementation. Depending on your specific use case, you might want to add
more features or optimizations, such as object recycling, resizing the pool dynamically, etc.

write a simple c# unity 3d script for hash map

In C#, you can use a Dictionary to implement a hash map. Here's a simple script
demonstrating how to use a Dictionary in Unity:
csharp Copy code

using System.Collections.Generic;
using UnityEngine;

public class SimpleHashMap : MonoBehaviour


{
// Define a dictionary with string keys and int values
private Dictionary<string, int> myHashMap = new Dictionary<string

void Start()
{
// Adding key-value pairs to the dictionary
myHashMap.Add("apple", 10);
myHashMap.Add("banana", 20);
myHashMap.Add("cherry", 30);

// Accessing values using keys


int appleCount = myHashMap["apple"];
int bananaCount = myHashMap["banana"];
int cherryCount = myHashMap["cherry"];

Debug.Log("Number of apples: " + appleCount);


Debug.Log("Number of bananas: " + bananaCount);
Debug.Log("Number of cherries: " + cherryCount);

// Checking if a key exists


if (myHashMap.ContainsKey("apple"))
{
Debug.Log("The key 'apple' exists in the hashmap.");
}

// Removing a key-value pair


myHashMap.Remove("banana");

// Checking if a key exists after removal


if (!myHashMap.ContainsKey("banana"))
{
Debug.Log("The key 'banana' doesn't exist in the hashmap anymor
}
}
}

In this script, we're using a Dictionary<string, int> to create a simple hash map
where the keys are strings (fruit names) and the values are integers (counts).
Please make sure to attach this script to a GameObject in your Unity scene. When you run
the scene, it will print out the counts of apples, bananas, and cherries, and demonstrate
adding, accessing, and removing items from the hash map.

create an unity 3d article with script examples on the types of lerps in unity

Understanding Different Types of Lerps in Unity

Introduction
In Unity, Lerp stands for linear interpolation, a technique used to smoothly transition
between two values over a specified time. There are several types of lerps available, each
with its unique characteristics. In this article, we'll explore some of the most commonly used
lerps in Unity and provide practical script examples for each.

1. Linear Lerp
Linear lerp provides a straightforward interpolation between two values. It moves from the
start value to the end value at a constant rate.
csharp Copy code

using UnityEngine;

public class LinearLerpExample : MonoBehaviour


{
public Transform startTransform;
public Transform endTransform;
public float lerpSpeed = 0.5f;

void Update()
{
float t = Mathf.PingPong(Time.time * lerpSpeed, 1.0f);
transform.position = Vector3.Lerp(startTransform.position, endTrans
}
}

In this example, startTransform and endTransform define the start and end positions.
The lerpSpeed controls the interpolation speed. The object's position is updated using
Vector3.Lerp.

2. Spherical Lerp
Spherical lerp, also known as Slerp, is used for interpolating rotations. It ensures the shortest
path is taken along the surface of a sphere.
csharp Copy code

using UnityEngine;

public class SphericalLerpExample : MonoBehaviour


{
public Transform startTransform;
public Transform endTransform;
public float lerpSpeed = 0.5f;

void Update()
{
float t = Mathf.PingPong(Time.time * lerpSpeed, 1.0f);
transform.rotation = Quaternion.Slerp(startTransform.rotation, endT
}
}
Here, startTransform and endTransform are rotation values. Quaternion.Slerp is
used to interpolate between them.

3. SmoothStep Lerp
SmoothStep lerp provides a smoother transition compared to linear lerp. It applies a smooth
curve to the interpolation.
csharp Copy code

using UnityEngine;

public class SmoothStepLerpExample : MonoBehaviour


{
public float startValue = 0f;
public float endValue = 10f;
public float lerpSpeed = 0.5f;

void Update()
{
float t = Mathf.PingPong(Time.time * lerpSpeed, 1.0f);
float result = Mathf.SmoothStep(startValue, endValue, t);
Debug.Log("Result: " + result);
}
}

In this example, Mathf.SmoothStep is used to interpolate between startValue and


endValue.

Conclusion
Understanding and utilizing different types of lerps in Unity is essential for creating smooth
and dynamic animations and transitions. By incorporating these techniques into your
projects, you can enhance the visual appeal and user experience of your games.
Remember to experiment with different lerp types and parameters to achieve the desired
effects for your specific use cases. Happy coding!

make above article a detailed one with all the types of lerps

Understanding Different Types of Lerps in Unity


Linear Interpolation, commonly referred to as Lerp, is a fundamental mathematical operation
used in computer graphics and game development. In Unity, lerping is a crucial technique for
smoothly transitioning between two values. There are several types of lerps available, each
serving different purposes. In this article, we will explore various types of lerps and provide
practical script examples for each.

Table of Contents
1. Linear Lerp
2. Spherical Lerp (Slerp)
3. SmoothStep Lerp
4. LerpAngle
5. LerpUnclamped
6. InverseLerp
7. LerpPrecise

1. Linear Lerp
Linear lerp provides a straightforward interpolation between two values. It moves from the
start value to the end value at a constant rate.
csharp Copy code

using UnityEngine;

public class LinearLerpExample : MonoBehaviour


{
public Transform startTransform;
public Transform endTransform;
public float lerpSpeed = 0.5f;

void Update()
{
float t = Mathf.PingPong(Time.time * lerpSpeed, 1.0f);
transform.position = Vector3.Lerp(startTransform.position, endTrans
}
}

In this example, startTransform and endTransform define the start and end positions.
The lerpSpeed controls the interpolation speed. The object's position is updated using
Vector3.Lerp.

2. Spherical Lerp (Slerp)


Spherical lerp, also known as Slerp, is used for interpolating rotations. It ensures the
shortest path is taken along the surface of a sphere.
csharp Copy code

using UnityEngine;

public class SphericalLerpExample : MonoBehaviour


{
public Transform startTransform;
public Transform endTransform;
public float lerpSpeed = 0.5f;

void Update()
{
float t = Mathf.PingPong(Time.time * lerpSpeed, 1.0f);
transform.rotation = Quaternion.Slerp(startTransform.rotation, endT
}
}

Here, startTransform and endTransform are rotation values. Quaternion.Slerp is


used to interpolate between them.

3. SmoothStep Lerp
SmoothStep lerp provides a smoother transition compared to linear lerp. It applies a smooth
curve to the interpolation.
csharp Copy code

using UnityEngine;

public class SmoothStepLerpExample : MonoBehaviour


{
public float startValue = 0f;
public float endValue = 10f;
public float lerpSpeed = 0.5f;

void Update()
{
float t = Mathf.PingPong(Time.time * lerpSpeed, 1.0f);
float result = Mathf.SmoothStep(startValue, endValue, t);
Debug.Log("Result: " + result);
}
}

In this example, Mathf.SmoothStep is used to interpolate between startValue and


endValue.

4. LerpAngle
LerpAngle is specifically designed for interpolating angles. It ensures the shortest path is
taken, considering the circular nature of angles.
csharp Copy code

using UnityEngine;

public class LerpAngleExample : MonoBehaviour


{
public float startAngle = 0f;
public float endAngle = 360f;
public float lerpSpeed = 0.5f;

void Update()
{
float t = Mathf.PingPong(Time.time * lerpSpeed, 1.0f);
float result = Mathf.LerpAngle(startAngle, endAngle, t);
Debug.Log("Result: " + result);
}
}

5. LerpUnclamped
LerpUnclamped allows interpolation beyond the usual 0 to 1 range. This can be useful for
situations where you want values outside this range.
csharp Copy code

using UnityEngine;

public class LerpUnclampedExample : MonoBehaviour


{
public float startValue = 0f;
public float endValue = 10f;
public float t = 2.5f;

void Start()
{
float result = Mathf.LerpUnclamped(startValue, endValue, t);
Debug.Log("Result: " + result);
}
}

6. InverseLerp
InverseLerp is used to find the interpolation parameter t that produces a given value within
the range of a and b.
csharp Copy code

using UnityEngine;

public class InverseLerpExample : MonoBehaviour


{
public float startValue = 0f;
public float endValue = 10f;
public float resultValue = 5f;

void Start()
{
float t = Mathf.InverseLerp(startValue, endValue, resultValue);
Debug.Log("t: " + t);
}
}

7. LerpPrecise
LerpPrecise provides a more precise interpolation compared to the standard lerp function.
It's useful in situations where high precision is required.
csharp Copy code

using UnityEngine;

public class LerpPreciseExample : MonoBehaviour


{
public float startValue = 0f;
public float endValue = 10f;
public float t = 0.5f;

void Start()
{
float result = Mathf.LerpPrecise(startValue, endValue, t);
Debug.Log("Result: " + result);
}
}

Conclusion
Understanding and utilizing different types of lerps in Unity is essential for creating smooth
and dynamic animations and transitions. By incorporating these techniques into your
projects, you can enhance the visual appeal and user experience of your games.
Remember to experiment with different lerp types and parameters to achieve the desired
effects for your specific use cases. Happy coding!

Continue this conversation

Report content|Terms of use|Privacy policy

You might also like