Impact CFX Main Page Index API Reference Change Logs
Legacy Impact Comparison Conversion From Legacy Impact
Impact CFX Project Settings Impact Tags Logging Impact Material Registry
Impact CFX Manager Material Mapping Managing Registered Materials
Impact Materials
Audio Effect Audio Source Prefab Audio Effect Processor Particle Effect Particle System Prefab Particle Effect Processor Decal Effect Decal Prefab Decal Effect Processor URP & HDRP Decals VFX Batch Effect VFX Batch Master Creating a Visual Effect VFX Batch Effect Processor Managing Attached Effects
Impact Objects Impact Object Single Material Impact Object Rigidbody Impact Object Rigidbody (Cheap) Impact Object Custom Velocity Objects With Multiple Materials Impact Terrain
Impact Triggers Common Trigger Properties Material Count Impact Collision Trigger Impact Slide and Roll Trigger Impact Speculative Collision Trigger Impact Simple Collision Trigger Impact Particle Collision Trigger Impact On Trigger Enter Impact On Trigger Stay
Impact Raycasting
FMOD Integration Wwise Integration Master Audio Integration
Custom Triggers Custom Objects Creating an Impact Object Script Impact Material Processors Custom Effects Impact Effect Authoring Asset Effect Result Effect Data Impact Effect Processors Impact Simple Effect Processor Impact Pooled Effect Processor Pooled Effect Object Pooled Effect Authoring Pooled Effect Result Pooled Effect Data Object Pool Pooled Effect Processor Add to Impact CFX Manager Example Effect Download

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;
		}
	}
}