After pressing the left mouse button, hold the bullet, change the sprite and choose the rotation (place where it will fire)
After pressing the right mouse button, shoot it.
My problem? The bullet after shooting doesn’t bounce off the walls and doesn’t rotate (face) in the moving direction.
My script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewFollow : MonoBehaviour {
public Sprite sprite1; // Drag your first sprite here
public Sprite sprite2;
public float moveSpeed = 10f;
bool mouseClicked = false;
private SpriteRenderer spriteRenderer;
bool rightClicked = false;
private bool canClick = true;
public float speed;
public Rigidbody2D rb;
// Use this for initialization
void Start ()
{
spriteRenderer = GetComponent<SpriteRenderer>(); // we are accessing the SpriteRenderer that is attached to the Gameobject
if (spriteRenderer.sprite == null) // if the sprite on spriteRenderer is null then
spriteRenderer.sprite = sprite1; // set the sprite to sprite1
rb = GetComponent<Rigidbody2D>();
}
Coroutine changeSpriteRoutine;
WaitForSeconds wfs = new WaitForSeconds(0.05f);
void OnCollisionEnter2D (Collision2D whatHitMe)
{
GameObject g = whatHitMe.gameObject;
if (g.CompareTag("Background") || g.CompareTag("Enemy"))
{
if(changeSpriteRoutine == null)
changeSpriteRoutine = StartCoroutine(ChangeTheDamnSprite());
{
if (g.CompareTag("Background") || g.CompareTag("Enemy"))
{
float angle = Mathf.Atan2(rb.velocity.y, rb.velocity .x) * Mathf.Rad2Deg;
rb.rotation = angle - 90;
}
}
}
}
IEnumerator ChangeTheDamnSprite() {
spriteRenderer.sprite = sprite2;
yield return wfs;
spriteRenderer.sprite = sprite1;
changeSpriteRoutine = null;
}
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 (canClick)
{
if (Input.GetMouseButtonDown (0)) {
Debug.Log ("Left Mouse Button was pressed");
moveSpeed = 0f;
if (spriteRenderer.sprite == sprite1) { // if the spriteRenderer sprite = sprite1 then change to sprite2
spriteRenderer.sprite = sprite2;
}
mouseClicked = true; //register that the mouse has been clicked and the sprite is changed to sprite2
rightClicked = false;
canClick = false;
}
}
if (mouseClicked) { //checks if sprite has already been changed
Vector3 mousePos = Input.mousePosition; //gets the current mouse position on screen
int currentCase = 0;
//following does a case-check on the position of your mouse with respect to the sprite:
if (Camera.main.transform.position.x - (Screen.width/2) + mousePos.x < transform.position.x) {
if (Camera.main.transform.position.x - (Screen.height/2)+mousePos.y < transform.position.y) {
currentCase = 2;
} else {
currentCase = 3;
}
} else {
if (Camera.main.transform.position.x - (Screen.height/2)+mousePos.y < transform.position.y) {
currentCase = 1;
} else {
currentCase = 0;
}
}
//create a new rotation:
transform.rotation = Quaternion.Euler (0, 0, 90 * currentCase);
}
{
if (Input.GetMouseButton(1)) {
Debug.Log ("Pressed secondary button.");
rightClicked = true;
mouseClicked = false;
}
if(rightClicked)
transform.position += (transform.right + transform.up).normalized * speed * Time.deltaTime;
}
}
}
}
Could anyone fix my script? I am a bit desperate because I am improving it for weeks and I can not achieve anything.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewFollow : MonoBehaviour {
public Sprite sprite1; // Drag your first sprite here
public Sprite sprite2;
public float moveSpeed = 10f;
bool mouseClicked = false;
private SpriteRenderer spriteRenderer;
bool rightClicked = false;
private bool canClick = true;
public float speed;
public Rigidbody2D rb;
float constantSpeed = 10f;
// stored in fixed update, to be used in OnCollisionEnter (which may have an altered value).
Vector2 currSpeed;
// Use this for initialization
void Start ()
{
spriteRenderer = GetComponent<SpriteRenderer>(); // we are accessing the SpriteRenderer that is attached to the Gameobject
if (spriteRenderer.sprite == null) // if the sprite on spriteRenderer is null then
spriteRenderer.sprite = sprite1; // set the sprite to sprite1
rb = GetComponent<Rigidbody2D>();
}
Coroutine changeSpriteRoutine;
WaitForSeconds wfs = new WaitForSeconds(0.05f);
void OnCollisionEnter2D (Collision2D whatHitMe)
{
GameObject g = whatHitMe.gameObject;
if (g.CompareTag("Background") || g.CompareTag("Enemy"))
{
if(changeSpriteRoutine == null)
changeSpriteRoutine = StartCoroutine(ChangeTheDamnSprite());
{
if (g.CompareTag("Background") || g.CompareTag("Enemy"))
{
rb.velocity = Vector2.Reflect(currSpeed, whatHitMe.contacts[0].normal);
rb.rotation +=90;
}
}
}
}
IEnumerator ChangeTheDamnSprite() {
spriteRenderer.sprite = sprite2;
yield return wfs;
spriteRenderer.sprite = sprite1;
changeSpriteRoutine = null;
}
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 ();
currSpeed = rb.velocity = constantSpeed * (rb.velocity.normalized);
{
if (canClick)
{
if (Input.GetMouseButtonDown (0)) {
Debug.Log ("Left Mouse Button was pressed");
moveSpeed = 0f;
if (spriteRenderer.sprite == sprite1) { // if the spriteRenderer sprite = sprite1 then change to sprite2
spriteRenderer.sprite = sprite2;
}
mouseClicked = true; //register that the mouse has been clicked and the sprite is changed to sprite2
rightClicked = false;
canClick = false;
}
}
if (mouseClicked) { //checks if sprite has already been changed
Vector3 mousePos = Input.mousePosition; //gets the current mouse position on screen
int currentCase = 0;
//following does a case-check on the position of your mouse with respect to the sprite:
if (Camera.main.transform.position.x - (Screen.width/2) + mousePos.x < transform.position.x) {
if (Camera.main.transform.position.x - (Screen.height/2)+mousePos.y < transform.position.y) {
currentCase = 2;
} else {
currentCase = 3;
}
} else {
if (Camera.main.transform.position.x - (Screen.height/2)+mousePos.y < transform.position.y) {
currentCase = 1;
} else {
currentCase = 0;
}
}
//create a new rotation:
transform.rotation = Quaternion.Euler (0, 0, 90 * currentCase);
}
{
if (Input.GetMouseButton(1)) {
Debug.Log ("Pressed secondary button.");
rightClicked = true;
mouseClicked = false;
}
if(rightClicked)
transform.position += (transform.right + transform.up).normalized * speed * Time.deltaTime;
}
}
}
}
I prepared a few code versions and in the best option(this code above), it looked like this:
I’d really start by cleaning up your code, remove any unnecessary brackets and fix your indentation. Then add some debugging, put a debug log in your OnCollisionEnter to see when you reflect and when you dont. You can even use OnCollisionStay to determine when you keep colliding with the edges.
I made a smaller circle collider that I had added to the face (in video YELLOW part) of the bullet and now it is not stuck, but the bullet falls out of the screen.
I added Debu.Log to check and it detects everything - every hit and rotation.
The same situation is when I exchange circle collider 2d for polygon collider 2d.
Whats this for ?
If you get wrong normal direction for what ever reason multiply it with -1.0f.
Also consider reading the description of functions witch say you should use GetContacts instead of of contacts.
ContactPoint2D[] myContact = new ContactPoint2D[1];
void OnCollisionEnter2D (Collision2D collider)
{
collider.GetContacts(myContact);
//do stuff with myContact[0].normal;
//or if the behavior is mirrored with (myContact[0].normal * -1.0f)
}
Thanks for this script, but I will look for another option.
Sending rigidbody 2d to kinematic, will cause that the bullet won’t bounce off the walls and I will have to make many changes in the script.
In addition, the script has disabled bouncing with 90 degrees in the direction of movement after each hit.
However, thank you for the script, maybe someday I will use it
Actually the script lets the bullet bounce off walls it collides with.
I also still do not get why you want to use + 90 degrees if your bullet is moving at a wall with a direction of 315 you will rotate it to 405 degree. Or when it fly’s straight up at 0 degree and you rotate it +90 degrees it will fly parallel with the wall at 90 degree…
Only cause it looks more wrong without +90 it wont make +90 the right way to do it also it always moves top right cause the direction is set to 0.7,0.7… (cos and sin of 45°)
Also the script is there to show you how to implement a bouncing object not to replace your whole code…