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 Apr 24, 2018 at 10:58 AM by polan31 for the following reason:

Other

avatar image
0
Question by polan31 · Apr 20, 2018 at 08:00 AM · movemouseclick

Why after the second mouse click the object doesn't start to move?

Hi everyone.

I hope I found the right place and that someone will help me solve the problem.

At the beginning I'll write that I have only spent two months with the language of c #.

I have a problem with my script.

I have an object that I call "BULLET".

After pressing the mouse on the BULLET, it rotates in four directions, following the mouse cursor

(MOUSE RIGHT DOWN- OBJECT ROTATE RIGHT DOWN ETC.) .

I added an option so that after the second mouse click on the object, the ball fired in the direction of rotation and began to bounce off the walls.

I really do not understand why after the second mouse click the object does not start to move.

Can someone find a solution?

My code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MouseChange : MonoBehaviour {
 
     private Transform _bullet;
     private Vector2 _screenSize;
     private float _interval = 10;
     public Sprite sp1,sp2;
     SpriteRenderer sr;
     bool mouseClicked = false;
     public float speed;
     bool firstClick = false;
     bool secondClick = false;
     // Use this for initialization
 
     void Start () 
     {
         sr = gameObject.GetComponent<SpriteRenderer> ();
 
         if (sr.sprite == null) // if the sprite on spriteRenderer is null then
             sr.sprite = sp1; // set the sprite to sprite1
 
         _bullet = transform;
         _screenSize = new Vector2(Screen.width, Screen.height);
     }
 
     public void Update ()
     {
         if (Input.GetMouseButtonDown (0)) {
             Debug.Log ("Left Mouse Button was pressed");
             if (sr.sprite == sp1) { // if the spriteRenderer sprite = sprite1 then change to sprite2
                 sr.sprite = sp2;
                 mouseClicked = true; //register that the mouse has been clicked and the sprite is changed to sprite2
             }
         }
     
             
         if (mouseClicked) {    
             {
                 if (!firstClick) {
                     firstClick = true;
                 } else if (!secondClick) {
                     secondClick = true;
                 }
             }
 
             if (firstClick && !secondClick) {
                 // Check for every 10 frames, reduce _interval if you want to check more often.
                 if (Time.frameCount % _interval == 0) {
                     // Use debug to learn mouse position, you can disable this if you want.
                     Debug.Log (Input.mousePosition);
 
                     // Check for every 10 frames, reduce _interval if you want to check more often.
                     if (Time.frameCount % _interval == 0) {
                         // Use debug to learn mouse position, you can disable this if you want.
                         Debug.Log (Input.mousePosition);
 
                         // Mouse on Top Right screen
                         _bullet.localEulerAngles = Vector3.zero;
 
                         // Mouse on Bottom Right, else Mouse on Bottom Left else Mouse on Top Left
                         if (Input.mousePosition.x > _screenSize.x / 2 && Input.mousePosition.y < _screenSize.y / 2)
                             _bullet.Rotate (Vector3.forward, -90, Space.Self);
                         else if (Input.mousePosition.x < _screenSize.x / 2 && Input.mousePosition.y < _screenSize.y / 2)
                             _bullet.Rotate (Vector3.forward, -180, Space.Self);
                         else if (Input.mousePosition.x < _screenSize.x / 2 && Input.mousePosition.y > _screenSize.y / 2)
                             _bullet.Rotate (Vector3.forward, 90, Space.Self);
                     } else if (secondClick) {
                         _bullet.transform.position += (_bullet.transform.right + _bullet.transform.up).normalized * speed * Time.deltaTime;
                     }
                 }
             }
         }
     }
 }
 
 

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

Answer by Priyanka-Rajwanshi · Apr 20, 2018 at 10:33 AM

Hi @ polan31 There are alot of issues in your code. To answer your question, as to why your sprite does not move is because your condition never gets satisfied: Your condition

   else if (secondClick) {

lies inside the condition

  if (firstClick && !secondClick) {

You enter into first condition when secondClick = false. Thus the second condition to check for secondClick to be true can never be satisfied.

Apart from this, if you check, your variable firstClick and secondClick would be made true in simultaneous updates without pressing the mouse button again. Thus it should be:

     if (Input.GetMouseButtonDown(0))
     {
         Debug.Log("Left Mouse Button was pressed");
         if (sr.sprite == sp1)
         { // if the spriteRenderer sprite = sprite1 then change to sprite2
             sr.sprite = sp2;
             if (!firstClick) {
                  firstClick = true;
              } else if (!secondClick) {
                  secondClick = true;
                  //There might be a need to make firstClick = false here!
              }
             mouseClicked = true; //register that the mouse has been clicked and the sprite is changed to sprite2
         }
     }

I am also not sure why you have used the following condition twice

     if (Time.frameCount % _interval == 0)

I would suggest you to look into your code deeply.

Comment
Add comment · Show 4 · 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 · Apr 21, 2018 at 07:38 PM 0
Share

Hey

I tried your code, however, it does not work.

To be sure what is wrong, I tried to do a right click ins$$anonymous$$d of the second click.

However, now it looks like this: (video link)

link text

Ins$$anonymous$$d of starting to move and bounce off the walls (free movement) the ball only makes a small movement.

avatar image polan31 polan31 · Apr 21, 2018 at 07:39 PM 0
Share
         void Start ()
         {
             sr = gameObject.GetComponent<SpriteRenderer> ();
  
             if (sr.sprite == null) // if the sprite on spriteRenderer is null then
                 sr.sprite = sp1; // set the sprite to sprite1
  
             _bullet = transform;
             _screenSize = new Vector2(Screen.width, Screen.height);
              }
              public void Update ()
     {
  
         transform.position = Vector2.Lerp (transform.position, Camera.main.ScreenToWorldPoint (Input.mousePosition), moveSpeed);
  
         Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
         difference.Normalize ();
  
         {
             if (Input.Get$$anonymous$$ouseButtonDown (0)) {
                 Debug.Log ("Left $$anonymous$$ouse Button was pressed");
                 moveSpeed = 0f;
                 if (sr.sprite == sp1) { // if the spriteRenderer sprite = sprite1 then change to sprite2
                     sr.sprite = sp2;
                              }
                 mouseClicked = true; //register that the mouse has been clicked and the sprite is changed to sprite2
             }
         }
                   if (mouseClicked) {        //checks if sprite has already been changed
  
             // Check for every 10 frames, reduce _interval if you want to check more often.
             if (Time.frameCount % _interval == 0) {
                 // Use debug to learn mouse position, you can disable this if you want.
                 Debug.Log (Input.mousePosition);
  
                 // $$anonymous$$ouse on Top Right screen
                 _bullet.localEulerAngles = Vector3.zero;
  
                 // $$anonymous$$ouse on Bottom Right, else $$anonymous$$ouse on Bottom Left else $$anonymous$$ouse on Top Left
                 if (Input.mousePosition.x > _screenSize.x / 2 && Input.mousePosition.y < _screenSize.y / 2)
                     _bullet.Rotate (Vector3.forward, -90, Space.Self);
                 else if (Input.mousePosition.x < _screenSize.x / 2 && Input.mousePosition.y < _screenSize.y / 2)
                     _bullet.Rotate (Vector3.forward, -180, Space.Self);
                 else if (Input.mousePosition.x < _screenSize.x / 2 && Input.mousePosition.y > _screenSize.y / 2)
                     _bullet.Rotate (Vector3.forward, 90, Space.Self);
             }
         }
              {
                      
             if (Input.Get$$anonymous$$ouseButtonDown (1)) {
                 Debug.Log ("Pressed secondary button.");
  
                           _bullet.transform.position += (_bullet.transform.right + _bullet.transform.up).normalized * speed * Time.deltaTime;
             }
         }
     }
 }
avatar image polan31 polan31 · Apr 21, 2018 at 07:41 PM 0
Share

Also before Start ():

    using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
      
     public class TestFollow : $$anonymous$$onoBehaviour {
      
             private Transform _bullet;
             private Vector2 _screenSize;
             private float _interval = 10;
             public Sprite sp1,sp2;
             SpriteRenderer sr;
             bool mouseClicked = false;
             public float moveSpeed = 10f;
             public float speed;
     

Show more comments

Follow this Question

Answers Answers and Comments

79 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

Related Questions

Cannot select items in Scene window 0 Answers

How to move object to the position of another object while clicked on it? 1 Answer

how to move or fade GUITextures with iTween 2 Answers

How do you make an object go left and right? 1 Answer

Top-Down Camera Trouble 1 Answer


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