Movement around a huge object by avoiding obstacles

Hello People,
I am working on a game where a NonPlayable Character takes a journey across a trunk of the tree.


I have a huge object in the middle. It’s height across the y axis is huge and it is the trunk. So, I could say it is like a big tree and small bugs flying around it. At any point, I would like the bug to travel around the tree in a spectacular fashion and hence the camera would be following it. It is completely automated.

The calculation for the path has to be during runtime with the constraint being to avoid the branches & trunk. Mostly the motion would be circular.


What would be the efficient way or the best approach to achieve it?

Thank you and appreciate your help.

I created something similar with Raycasts, but, it will require you to do a lot of polishing to get the effect to look very realistic. But none the less, the object will avoid obstacles, but requires a Target be set . . .

With having that said, I suggest using this only as a starting point. It contains most of what you need for a fully fledged obstacle avoidance .

Currently, it supports obstacle avoidance forward, left, and right, but does not work if there are colliders on both the left and right side. . . You can change this, and fix it up a bit though.

PS: Feel free to use this script in whatever way you want .

Here you go:

#pragma strict

var Target : GameObject ;

var TargetHit : RaycastHit ;
var TargetHitLeft : RaycastHit ;
var TargetHitBack : RaycastHit ;



class _VisionRaycastHit{
	var Back : RaycastHit ;
	var Front : RaycastHit ;
	var Left : RaycastHit ;
	var Right : RaycastHit;
}

class _VisionDirection {
	var Left : float = 0 ;
	var Right : float = 0 ;
	
	var LeftActive : boolean = false ;
	var RightActive : boolean = false ;
}

class _Vision {
	var Back : boolean = false ;
	var Front : boolean = false ;
	var Left : boolean = false ;
	var Right : boolean = false ;
	
	var EaseOut : float = 2 ; 
	
	var Hit : _VisionRaycastHit ;
	
	var VisionDirection : _VisionDirection ;
}

var Vision : _Vision = new _Vision ( ) ;


function Start () {

}

function Update () {

	Vision.Front = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.forward ), Vision.Hit.Front, 15);
	Vision.Back = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.back ), Vision.Hit.Back, 15);
	Vision.Left = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.left ), Vision.Hit.Left, 15);
	Vision.Right = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.right ), Vision.Hit.Right, 15);
	//VisionLeft = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.left), TargetHitLeft, 15);
	//VisionBack = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.forward ), TargetHitBack, 15);
	var Direction : Vector3 ;
	
	Direction = ( transform.TransformDirection ( Vector3.forward ) ) ;
	
	if ( Vision.Front )
	{
		if ( Vision.Left )
		{
			Vision.VisionDirection.Right += 5 * Time.deltaTime ;
			Vision.VisionDirection.RightActive = true ;
		}else if ( Vision.Right ){
			Vision.VisionDirection.Left += 5 * Time.deltaTime ;
			Vision.VisionDirection.LeftActive = true ;
		}
		
		if ( Vision.VisionDirection.LeftActive ){
			transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x-Vision.VisionDirection.Left , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
			//Vision.EaseOut = 245 ;
	
		}else if ( Vision.VisionDirection.RightActive ){ // If Turning Right
			transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x+Vision.VisionDirection.Right , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
			Vision.EaseOut = 2 ;
	
		}else{ // Turn Right By Default 
			transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x+Vision.VisionDirection.Right , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
			Vision.EaseOut = 2 ;
	
		}
	}else{
		
		transform.position += Direction * Time.deltaTime ; 
		if ( Vision.Right == false && Vision.Left == false ){
			if ( Mathf.Ceil ( Vision.EaseOut ) == 0  ){
				Vision.VisionDirection.LeftActive = false ;
				Vision.VisionDirection.RightActive = false ;
			}else{
				Vision.EaseOut -= 1 * Time.deltaTime ;
			}
		}
		
		if ( !Vision.VisionDirection.RightActive && !Vision.VisionDirection.LeftActive ){
			
			///transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
		}
		
		if ( Vision.Right )
		{
			Vision.EaseOut = 2 ;
		}else if ( Vision.Left ){
			Vision.EaseOut = 2 ;
		}
	}
}