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.