Issues with Collision

Hola! I’m trying to make a levitation script. There’s a glitch with the scroll wheel where the player can force an object through a wall. I tried using SphereCast to restrict the Z variable: AddZ. No success.

Here’s the fail code:

using UnityEngine;
using System.Collections;

public class telekinesis : MonoBehaviour 
{
targetz
        void Update()
{
			MousePoint = new Vector3(Screen.width/2, Screen.height/2, AddZ);
			WhereTo = Camera.main.ScreenToWorldPoint(MousePoint);
			//moving time
			TargetMem.rigidbody.velocity = (WhereTo-target.position)*Speed;
			
			if (Input.GetAxis("Mouse ScrollWheel") != 0)
			{
				if (!Physics.SphereCast(Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2,Screen.height/2,AddZ+Input.GetAxis("Mouse ScrollWheel")*.05f)),target.localScale.x/2,Vector3.zero, out hit))
				{
					AddZ += Input.GetAxis("Mouse ScrollWheel")*5;
				}
				else
				{
					AddZ = Vector3.Distance(target.position, transform.position);
				}
			}
			
			if (Input.GetKey(KeyCode.LeftShift))
			AddZ += .05F;
			else if (Input.GetKey(KeyCode.LeftControl))
			AddZ -= .05F;
			
			
			//Making some pretty limits
			
			if (AddZ < 2.5F)
			{
				AddZ = 2.5F;
			}
			
			
		}
	}
}

What exactly am I doing wrong?

Okay, I updated the code to Raycast. Still nothing.

if (!Physics.Raycast(Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2,Screen.height/2,AddZ)), Vector3.forward, 5) || !Physics.Raycast(Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2,Screen.height/2,AddZ)), Vector3.back, 5))
{
	AddZ += Input.GetAxis("Mouse ScrollWheel")*5;
}
else
{
	AddZ = Vector3.Distance(target.position, transform.position);
}

before using the script has proven using Continuous Dynamic collision detection?

Turned on continuous dynamic in Rigidbody. Doesn’t seem to make much of a difference. This new code seems to work, but for some reason the Z speed isn’t consistent.

//moving time
			TargetMem.rigidbody.velocity = (WhereTo-target.position)*Speed;
			
			
			if ((!Physics.Raycast(new Vector3(target.position.x,target.position.y,target.position.z), Vector3.forward, 1)  Input.GetAxis("Mouse ScrollWheel") > 0))
			{
				AddZ += 5;
			}
			else if (!Physics.Raycast(new Vector3(target.position.x,target.position.y,target.position.z), Vector3.back, 1)  Input.GetAxis("Mouse ScrollWheel") < 0)
			{
				AddZ -= 5;
			}
			else
			{
				AddZ = Vector3.Distance(target.position, transform.position);
			}

There isn’t anything colliding anymore. I didn’t change the script at all.

Are your walls just planes with a meshCollider/ flat boxCollider’s?
Try using a boxCollider, and increasing the depth of the collider “behind” the wall, by adjusting the BoxCollider’s Size and Center offset.
If your collider is too thin, and an object travels fast enough, it can move far enough through it in one frame update to not trigger the expected collision…

Thanks, guys. I figured out how to code it:

if (!Physics.Raycast(new Vector3(Target.position.x,Target.position.y,Target.position.z), CamPos.forward, 1)  Input.GetAxis("Mouse ScrollWheel") > 0)
			{
				AddZ += 1;
			}
			else if (!Physics.Raycast(new Vector3(Target.position.x,Target.position.y,Target.position.z), -CamPos.forward, 1)  Input.GetAxis("Mouse ScrollWheel") < 0)
			{
				AddZ -= 1;
			}
			else if ((Physics.Raycast(new Vector3(Target.position.x,Target.position.y,Target.position.z), -CamPos.forward, 1)  Input.GetAxis("Mouse ScrollWheel") < 0) || (Physics.Raycast(new Vector3(Target.position.x,Target.position.y,Target.position.z), CamPos.forward, 1)  Input.GetAxis("Mouse ScrollWheel") > 0))
			{
				AddZ = Vector3.Distance(Target.position, transform.position);
			}

It works fine! But I’m having trouble with any other sort of movement. Whenever I walk, I can still force the held object through a wall.