0
\$\begingroup\$

2D Game in UNITY C#

I have an object BULLET .

After pressing the left mouse on my object (BULLET), the script allows me to rotate object according to the mouse's direction, in four directions :

-up right

-down right

-down left

-up left

I would like to add a movement so that the object starts moving in face direction after pressing the right mouse button.

I did it, but the object doesn't move freely in face direction and doesn't bounce off the walls, it just follows the mouse.

But I just want to fire my bullet.

I don't want the object to follow the mouse, just fire in face direction.

You know, like an arrow from a bow.

I press the left mouse button and I start rotate the object - THEN I press the right mouse button and the object flies without stopping in face direction, and began to bounce off the walls.

I show what's wrong on video (link below): enter link description here

This is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class TestFollow : MonoBehaviour {

    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;
    bool rightClicked = 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 ()
{

    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.GetMouseButtonDown (0)) {
            Debug.Log ("Left Mouse 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);

            // 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);
        }
    }

    {

        if (Input.GetMouseButton(1)) {
            Debug.Log ("Pressed secondary button.");
            rightClicked = true;
        }
        if(rightClicked)
            _bullet.transform.position += (transform.right + _bullet.transform.up).normalized * speed * Time.deltaTime;

    }
    }
}

PS. I'm sorry if I shouldn't post here, but I'm in a little depression. I'm working on this code for a few days and I can't change anything.

PS 2. sorry for my English. Not English-speaking country.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The code is a bit messy, but I think you'll want to set "mouseClicked" to false when you right-click. So in the Input.GetMouseButton(1) if statement:

if (Input.GetMouseButton(1)) {
    Debug.Log ("Pressed secondary button.");
    rightClicked = true;
    mouseClicked = false;
}

If you then want to fire the bullet again, you'll want to add the reverse in the left-click if statement. So you'll want to set "rightClicked" to false there.

At the moment it's continuously aiming because you're never telling it to stop doing so.

Edit:

As you said in your comment, you want to only be able to click the left mouse button once, then ignore it.

That can be achieved by adding a new boolean to check whether or not the left mouse button can be clicked. As displayed here:

private bool canClick = true;

if (canClick) {
        if (Input.GetMouseButtonDown (0)) {
            Debug.Log ("Left Mouse 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

            rightClicked = false;                
            canClick = false;
        }
    }
\$\endgroup\$
11
  • \$\begingroup\$ You know, I did it half an hour ago and it caused the bullet to move as it should. However, it create another problem: If you press the left button again, then the ball will start following the mouse again. Can I somehow block it so that after pressing the left button again, it doesn't change anything? \$\endgroup\$
    – Polan31
    Commented Apr 23, 2018 at 15:32
  • \$\begingroup\$ Yes, you can create another boolean that also changes to false when you press the left mouse button. Then in the Input.GetMouseButton(0) if statement, you add "&& !canClick" or something along those lines. I'll edit my answer to include this. \$\endgroup\$
    – basklein
    Commented Apr 23, 2018 at 15:46
  • \$\begingroup\$ I will be grateful \$\endgroup\$
    – Polan31
    Commented Apr 23, 2018 at 15:49
  • 1
    \$\begingroup\$ Thank you very much. I spent a few days on this problem, but now everything works great. In addition, you explained what and how. Thank you very much again. \$\endgroup\$
    – Polan31
    Commented Apr 23, 2018 at 16:01
  • \$\begingroup\$ No problem. If you have any more questions, don't hesitate to ask! But always remember to do some research by yourself first! Even just thinking about a problem enough to do the research, may lead to you finding the answer! \$\endgroup\$
    – basklein
    Commented Apr 23, 2018 at 17:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .