My object is an arrow.
Arrow should bounce off the walls.
After the collision, the object (sprite) must rotate in the direction of movement.
My problem is that sometimes the arrow instead of bouncing, for example, to the left - it bounces to the right, which makes it look very strange.
Does anyone know how to solve this problem?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestFollow : 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;
// 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>();
}
public void Update ()
{
rb.velocity = currSpeed;
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;
}
}
}
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"))
Debug.Log ("hit");
{
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;
}
}
Here is the video
https://gfycat.com/SoupyPointlessAmericanindianhorse