I have a script.
It allows me to move my object (with using mouse), after pressing the left mouse button to stop it, change the sprite and fire it after pressing the right mouse button.
My problem?
I don’t know and I don’t understand why this script disables bouncing off the wall.
I added material to the wall and objects, but nothing works.
I see something like this for the first time.
Could it be possible that the script has disabled bouncing?
How can I fix it?
In the attachments I put script, screen and video showing how it looks.
PS: Sorry for my english.
Video link:
(I know that the blue ball has no material.
I did it, because I didn’t hit it)
https://gfycat.com/pl/gifs/detail/AshamedPeriodicAdder
Screen:
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PILLController1 : MonoBehaviour {
private Transform _Pill;
private Vector2 _screenSize;
private float _interval = 10;
bool mouseClicked = false;
public float moveSpeed = 10f;
public float speed;
bool rightClicked = false;
private bool canClick = true;
public Sprite sprite1; // Drag your first sprite here
public Sprite sprite2; // Drag your second sprite here
private SpriteRenderer spriteRenderer;
// 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
_Pill = transform;
_screenSize = new Vector2(Screen.width, Screen.height);
}
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());
}
}
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
// 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
_Pill.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)
_Pill.Rotate (Vector3.forward, -90, Space.Self);
else if (Input.mousePosition.x < _screenSize.x / 2 && Input.mousePosition.y < _screenSize.y / 2)
_Pill.Rotate (Vector3.forward, -180, Space.Self);
else if (Input.mousePosition.x < _screenSize.x / 2 && Input.mousePosition.y > _screenSize.y / 2)
_Pill.Rotate (Vector3.forward, 90, Space.Self);
}
}
{
if (Input.GetMouseButton(1)) {
Debug.Log ("Pressed secondary button.");
rightClicked = true;
mouseClicked = false;
}
if(rightClicked)
_Pill.transform.position += (transform.right + _Pill.transform.up).normalized * speed * Time.deltaTime;
}
}
}
}