- Home /
 
              This question was 
             closed Nov 10, 2018 at 01:02 PM by 
             polan31 for the following reason: 
             
 
            The question is answered, right answer was accepted
 
               Question by 
               polan31 · Nov 10, 2018 at 12:52 PM · 
                objectnot workingmoving  
              
 
              Why is my object not moving?
I would like the object to start moving after pressing the left mouse button.
When I press left mose button nothing happens.
But if I delete : "
if(rightClicked){
" my object move per frame.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class KnifScript : MonoBehaviour {
 
     [SerializeField]
     public float speed;
     //knife shouldn't be controlled by the player when it's inactive 
     //(i.e. it already hit the log / another knife)
     private bool isActive = true;
 
     //for controlling physics
     private Rigidbody2D rb;
 
     bool rightClicked = false;
 
     private void Awake()
     {
         rb = GetComponent<Rigidbody2D>();
     
     }
 
     void Update () 
     {
         //this method of detecting input also works for touch
         if (Input.GetMouseButtonDown (0) && isActive) {
 
             bool rightClicked = true;
         }
         if(rightClicked)
         {
 
             MoveIn ();
         }
     }
 
     public void MoveIn (){
 
         transform.Translate (0, speed * Time.deltaTime, 0);
 
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         //we don't even want to detect collisions when the knife isn't active
         if (!isActive)
             return;
 
         //if the knife happens to be active (1st collision), deactivate it
         isActive = false;
 
         //collision with a log
         if (collision.collider.tag == "Log") {
 
 
             rb.velocity = new Vector3 (0, 0, 0);
             //this will automatically inherit rotation of the new parent (log)
             rb.bodyType = RigidbodyType2D.Kinematic;
             transform.SetParent (collision.collider.transform);
 
         
 
         }
     }
 
 }
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by edo_m18 · Nov 10, 2018 at 12:59 PM
GetMouseButtonDown called once when the mouse button down. You should use GetMouseButton method instead of it.
 if (Input.GetMouseButton(0) && isActive)
 {
     MoveIn();
 }