Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • 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

avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
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();
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image polan31 · Nov 10, 2018 at 01:02 PM 0
Share

no more questions :) thanks ;)

Follow this Question

Answers Answers and Comments

103 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to align particle systems to an objects orientation as it moves? 2 Answers

Moving an object 1 Answer

Model moving from Rotation only? 1 Answer

2D Sprite/object rotation changing despite telling it not to 0 Answers

How to I make an object move faster? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges