Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Handling Collisions unity

// Step 1: Create interface
public interface IHealth
{
	void TakeDamage(int damage);
}

// Step 2: Implement in Victim
public class Player : IHealth
{
	public void TakeDamage(int damage)
	{
		// handle when take damage
	}
}

// Step 3: Implement in Attacker
public class Bullet
{
	int damage = 100;
	public void OnCollisionEnter(Collision2D col)
	{
	    //check col => implement IHealth or not?
		var hp = col.gameObject.GetComponent<IHealth>();
		if (hp != null)
		{
			hp.TakeDamage(damage);
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: constant interpolated string 
Csharp :: conflictingactionsresolver as a workaround 
Csharp :: Max upload size for ASP.MVC CORE website 
Csharp :: how to change the color of a textbox with button c# 
Csharp :: How to return a list to view after foreach in c# 
Csharp :: how to hide tree level button when no record found for devexpress child grid view in Winform c# 
Csharp :: c# return statement 
Csharp :: how to do division with button C# 
Csharp :: asp net mvc convert ienumerable to selectlistitem 
Csharp :: C# Action Delegate 
Csharp :: how to extract data from a document using c# 
Csharp :: index sort 
Csharp :: .net entities query multiple join condition type inference 
Csharp :: UnityEngine.Mesh:get_vertices() 
Csharp :: enumerate dictionary c# 
Csharp :: display array value sin C# 
Csharp :: chrome devtools capture all styling an element uses 
Csharp :: telerik raddatepicker default date today wpf 
Csharp :: ask int text c# 
Csharp :: blazor navlink change bg of current component 
Csharp :: c# how to use or operator on integers in while loop 
Csharp :: Unity mousetoterrainposition 
Csharp :: unity how to set framrate C# 
Csharp :: c# single comment 
Csharp :: how to clone something as a parent unity 
Csharp :: Appending to an Existing CSV File with csvhelper 
Csharp :: Request ID: XPBBR4XG1UWuX6fWF08_-jzYkrommVJjO7Os50CTYuZmiw7kMsFUkw== 
Csharp :: c sharp switch forms 
Csharp :: unity move in x seconds to pos 
Csharp :: php check syntax error folder 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =