Detect Collisions While Dragging 3D Game Object
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);
}
}
Good day.
Did you debugged the code? Is the method On$$anonymous$$ouseDrag beeing called? What line is not working?
You should be able to detect what is not working if debug the code while running.
Good day.
Your answer

Follow this Question
Related Questions
HELP WITH COLLISIONS 0 Answers
OnCollisionEnter not running. 1 Answer
Line renderer's edge collider sometimes missing collision with object 0 Answers
How to drag objects by mouse when the camera is vertically above the terrain and facing it ? 0 Answers
Unity C# Casual Jewel Mining Game - Jewel Unaffected by Claw 1 Answer