- Home /
Other
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;
}
}
}
}
}
}
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.
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)
Ins$$anonymous$$d of starting to move and bounce off the walls (free movement) the ball only makes a small movement.
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;
}
}
}
}
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;