Moving object with raycast issues

Hi Unity Community,

I have written a script where a gameobject is intended to move to a raycast.point thrown from the player camera. For the most part this works fine, however there are times (approximately when camera is 45 degrees up from the object) when the object rapidly moves towards the camera (i.e. raycast source).

I have tried a number of approaches attempting to resolve this, however I can’t seem to dig out the root of this issue. If anyone can provide any pointers as to where I am going wrong I would be incredibly grateful.

NB: coding in uJS

Many thanks in advance,
Ryan

function FixedUpdate()
{

	if (modObj != null && !guiMode){
	
		//Panel Control 
		if (!selectObjPanel.activeSelf && !modifySelectObjPanel.activeSelf) //if the selectpanel not open and modSelect not already activated
		{
			activateModSelectObjPanel(true); //activate it
		} 
		else if (selectObjPanel.activeSelf)
		{
			activateModSelectObjPanel(false);
		}

	
		//Move
		if ( Input.GetKey(KeyCode.E))
		{	
			if (Input.GetKeyDown(KeyCode.E))
			{
//				modObj.GetComponent(BoxCollider).enabled = false;
				modObj.GetComponent(Rigidbody).isKinematic = true;
				modObj.GetComponent(Rigidbody).useGravity = false;
//				
				initPos = modObj.transform.position;
				var initRotation = modObj.transform.rotation;	
			}
	
			moveObject(modObj, initPos, initRotation);
		}
		else{
//			modObj.GetComponent(BoxCollider).enabled = true;
			modObj.GetComponent(Rigidbody).isKinematic = false;
			modObj.GetComponent(Rigidbody).useGravity = true;
		}
	}
}

function moveObject(modObj : GameObject, initPos : Vector3, initRotation : Quaternion)
{
	//Debug.Log("Moving Object");

	var hit : RaycastHit;
	var foundHit : boolean = false;
	
	foundHit = Physics.Raycast(transform.position, transform.forward, hit);
	//Debug.DrawRay(transform.position, transform.forward, Color.blue);
	
	if(foundHit && hit.transform.tag != "Player")
	{
		//Debug.Log("Move to Hit Point: " + hit.point);
		modifyObjGUIscript.activateMoveDisplay(initPos, hit.point);		
		
		var meshHalfHeight = modObj.GetComponent.<MeshRenderer>().bounds.size.y /2; //helps account for large and small objects
//		Debug.Log("CurObj Mesh Min: " + meshHalfHeight);
		
//		modObj.transform.position = hit.point; //***method 01***
//		modObj.transform.position = Vector3.Lerp(initPos, hit.point, speed); //***method 02***
//		modObj.transform.position = Vector3.SmoothDamp(initPos, hit.point, velocity, smoothTime); //***method 02***

		var rb = modObj.GetComponent.<Rigidbody>(); 
		rb.MovePosition(hit.point); //***method 03***
		
		modObj.transform.position.y =  modObj.transform.position.y + meshHalfHeight + hoverHeight;
		
		modObj.transform.rotation = initRotation;
	}
}

Turns out the issue was being caused by the raycast hitting the object being moved. Resolved this by only allowing hits from the terrain to be used as points to move to.

if(foundHit && hit.transform.tag == "Terrain")

The raycast hitting the object you are moving, causing the raycast’s point to move towards the camera. Then the object moves, causing the point to move towards the camera again. Your best bet to solving this issue is to assign a layer to the object you are creating, and then create a layermask and use it during your raycasting to ignore the object you are moving.