- Home /
Object movement, collider and Physics.
Hi there, having some trouble with the following code, I can move the cube but moving the mouse too fast means the object will stop until I reclick to start the movement again.
I fixed this by changing if (collider.Raycast( to if (Physics.Raycast( the problem with this solution was that now whenever I click anywhere on the game the cube can move which is not the functionality that I would like.
Some things to note, the script is attached to the playerCube and not global like the Camera.
Could anyone provide any help?
Thanks Rob
 void MovePlayer()
     {
         Ray ray;
         RaycastHit hit;
         
         if(Input.GetButtonDown("Fire1"))
         {
             clicked = true;
             
             ray = Camera.main.camera.ScreenPointToRay(Input.mousePosition);
             if (collider.Raycast(ray, out hit, float.MaxValue) && clicked == true)
             {
                 print("Player Selected");
             }
         }
         
         if(Input.GetButton("Fire1"))
         {
             float distance;
             
             ray = Camera.main.camera.ScreenPointToRay(Input.mousePosition);
             if (collider.Raycast(ray, out hit, float.MaxValue) && clicked == true)
             {
                 ray = Camera.main.camera.ScreenPointToRay(Input.mousePosition);
                 Plane plane = new Plane(this.transform.forward, this.gameObject.transform.position);
                 Vector3 positionIn3DSpace = Vector3.zero;
     
                 if (plane.Raycast(ray, out distance))
                 {
                     positionIn3DSpace = (distance * ray.direction) + ray.origin;
                     this.transform.position = positionIn3DSpace;
                 }
             }
             
             
         }
         
         if(Input.GetButtonUp("Fire1"))
         {
             clicked = false;
         }
     }
Your answer
 
 
             Follow this Question
Related Questions
Multiple colliders using IPointerEnter/ExitHandler 1 Answer
Raycast Without Colliders 6 Answers
[2D] Raycast to mouse cursor = offset on Y axis 0 Answers
Lag when I change the layer of a game object ingame 0 Answers
Raycast with strange Behaviour 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                