Hello i am making a game in which i have to drag a sphere on x axis and z axis on a plane with my mouse and i have created walls from cubes so that my sphere not go anywhere else in the game but while dragging my sphere is not detecting collisions with the wall i also added rigidbody but nothing is working ths is my code thanks
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour
{
Rigidbody r;
private void Start()
{
r = GetComponent<Rigidbody>();
}
void OnMouseDrag()
{
float distance_to_screen =
Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new
Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
transform.position = new Vector3(pos_move.x, transform.position.y,
pos_move.z);
}
}
is Trigger
to one your colliders?? \$\endgroup\$Transform
component is still teleportation from the perspective of the physics engine. If you want to avoid teleporting, you need to either "look before you leap" using a physics query to check that the desired travel path is clear before you move, or else route all movement through theRigidbody
and let the physics engine integrate it and handle collisions appropriately. \$\endgroup\$