- Home /
Mouse move question
I have a script to move my player with the position of the mouse. How can I make it stop if it hits a collider?
 var depth = 10.0;
  
 function Start ()
 {
      Screen.showCursor = false;
 }
  
 function Update ()
 {
      var mousePos = Input.mousePosition;
      var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth));
      transform.position = wantedPos;
 }
               Comment
              
 
               
              Add a OnTriggerXXXXXX or OnColliderXXXXXX method and a boolean switch that is evaluated in Update that if false, will allow you to move and true will not allow you to move(update the transform.position).
Edit:
Something like:
 var depth = 10.0;
 var moveable = true;
 
 function Start ()
 {
      Screen.showCursor = false;
 }
  
 function Update ()
 {
         if (moveable)
         {
      var mousePos = Input.mousePosition;
      var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth));
      transform.position = wantedPos;
         }
 }
 
 function OnCollisionEnter(collision : Collision) 
 {
         // some check to see if you hit what you think.
         if (collision.gameObject.tag.Equals("wall"))
         {
             moveable = false;
         }
 }
 
 function OnCollisionExist(collision: Collision)
 {
         if (collision.gameObject.tag.Equals("wall"))
         {
             moveable = true;
         }
 }
But you have to adapt it to your code and game objects.
Answer by revolute · Aug 27, 2014 at 12:41 PM
Use function OnCollisionEnter(collision : Collision) function in your script. It will be called provided that you have a collider on your gameobject as well as a rigidbody component attached to it. 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                