Impact Raycasting
This section provides some simple examples for raycasting with Impact CFX.
For comprehensive documentation of ImpactRaycaster, please refer to the API Reference.
ImpactRaycaster is a helper class that can assist in playing effects from raycasts. There are two types of methods available for both 3D and 2D raycasts:
- QueueRaycast - This method is used to play an effect using the provided Impact Material.
- QueueRaycastInverted - This method is used to play an effect using the object that was hit by the raycast.
Code Examples
This first example will play en effect when the user left-clicks.
//Inspector field for the Impact Material to associate with the raycast
public ImpactMaterialAuthoring Material;
private void Update()
{
	//When the player left-clicks, show a collision effect.
	if (Input.GetMouseButtonDown(0))
	{
		//Get a ray from the cursor position
		Ray cursorRay = Camera.main.ScreenPointToRay(Input.mousePosition);
		//Do a raycast
		if (Physics.Raycast(cursorRay, out RaycastHit raycastHit))
		{
			//A velocity to give to the raycast.
			float collisionVelocity = 10;
			//Call the QueueRaycast3D method with these parameters
			ImpactRaycaster.QueueRaycast3D(
				raycastHit,                                 //The raycast hit info
				cursorRay.direction * collisionVelocity,    //The velocity vector of the raycast
				Material,                                   //The Impact Material for the raycast
				CollisionType.Collision,                    //Use the single-collision type.
				0,                                          //Raycast ID is not needed for CollisionType.Collision
				1);                             			//The number of materials to get from the object that was hit.
		}
	}
}
			This example is similar to the first, but uses the Inverted method to play an effect using the object that was hit instead. Note the use of ImpactTagMask as a serialized inspector field.
//Inspector field for the Impact Tag Mask to associate with the raycast
public ImpactTagMask TagMask;
private void Update()
{
	//When the player left-clicks, show a collision effect.
	if (Input.GetMouseButtonDown(0))
	{
		//Get a ray from the cursor position
		Ray cursorRay = Camera.main.ScreenPointToRay(Input.mousePosition);
		//Do a raycast
		if (Physics.Raycast(cursorRay, out RaycastHit raycastHit))
		{
			//A velocity to give to the raycast.
			float collisionVelocity = 10;
			//Call the QueueRaycastInverted3D method with these parameters
			ImpactRaycaster.QueueRaycastInverted3D(
				raycastHit,                                 //The raycast hit info
				cursorRay.direction * collisionVelocity,    //The velocity vector of the raycast
				TagMask,                                    //The Impact Tags for the raycast
				CollisionType.Collision,                    //Use the single-collision type.
				0,                                          //Raycast ID is not needed for CollisionType.Collision
				2);                            				//The number of materials to get from the object that was hit.
		}
	}
}
			Finally, this last example demonstrates using raycasting for a sliding effect.
//Inspector field for the Impact Material to associate with the raycast
public ImpactMaterialAuthoring Material;
//The previous raycast hit point for determining sliding velocity
private Vector3 previousHitPoint;
//Use FixedUpdate because Update may run multiple times before FixedUpdate, resulting in duplicate effects.
private void FixedUpdate()
{
	//When the player holds right-click, show a slide effect.
	if (Input.GetMouseButton(1))
	{
		//Get a ray from the cursor position
		Ray cursorRay = Camera.main.ScreenPointToRay(Input.mousePosition);
		//Do a raycast
		if (Physics.Raycast(cursorRay, out RaycastHit raycastHit))
		{
			//Determine the velocity by subtracting the current hit point from the previous hit point.
			Vector3 dragVelocity = (raycastHit.point - previousHitPoint) / Time.deltaTime;
			ImpactRaycaster.QueueRaycast3D(
				raycastHit,             //The raycast hit info
				dragVelocity,           //The velocity vector of the raycast
				Material,               //The Impact Material for the raycast
				CollisionType.Slide,    //Use the slide collision type.
				GetInstanceID(),        //Raycast ID is needed here so that the slide audio can update properly. Use this script's instance ID.
				1);         			//The number of materials to get from the object that was hit.
			//Set previous hit point.
			previousHitPoint = raycastHit.point;
		}
	}
}
		