Hi,
I’m just starting with unity after a little tutorial (flappy bird of course xD) and I’m having some troubles with the behaviour of the sprite collision.
The main sprite has a BoxCollider2D and a Rigidbody2D (currently wit Gravity Scale to 0 and Is kinematic disabled because I move it with the arrow keys), and I have put some block sprites around the screen with BoxCollider2D and a Rigidbody2D (same config, no gravity and Is Kinematic disabled). When the main sprite is moved against a block and collides, the block is moved like being pushed and the main sprite makes a rebound… how could I change that and make the block sprite totally block the main sprite from moving forward?
On your blocks either remove Rigidbody2d or just freeze the rotation.
Source of the main sprite.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Bird : MonoBehaviour {
public float speed = 1;
public float force = 300;
private int score = 0;
private SpriteRenderer spriteRenderer;
// Use this for initialization
void Start () {
//GetComponent<Rigidbody2D>().velocity = Vector2.right * this.speed;
this.spriteRenderer = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update () {
bool touch = false;
int input = 0;
const int none = 0, up = 1, right = 2, down = 4, left = 8;
// Detect touch event
for (int i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
touch = true;
}
}
if (touch) {
input = none;
} else {
if (Input.GetKeyDown(KeyCode.UpArrow)) {
input = up;
} else {
if (Input.GetKeyDown(KeyCode.RightArrow)) {
input = right;
} else {
if (Input.GetKeyDown(KeyCode.DownArrow)) {
input = down;
} else {
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
input = left;
} else {
if (Input.GetMouseButtonDown(0)) {
input = none;
}
}
}
}
}
}
switch (input) {
// Object goes up
case up: {
float increment = this.speed;
this.transform.position = Vector3.Lerp(this.transform.position, this.transform.position + Vector3.up, increment);
}
break;
// Object goes right
case right: {
/*float increment = this.speed;
this.transform.position = Vector3.Lerp(this.transform.position, this.transform.position + Vector3.right, increment);*/
}
break;
// Object goes down
case down: {
float increment = this.speed;
this.transform.position = Vector3.Lerp(this.transform.position, this.transform.position + Vector3.down, increment);
}
break;
// Object goes left
case left: {
/*float increment = this.speed;
this.transform.position = Vector3.Lerp(this.transform.position, this.transform.position + Vector3.left, increment);*/
}
break;
// Do nothing
default: {
}
break;
}
// Exit on escape/android escape button
if (Input.GetKeyDown(KeyCode.Escape)) {
Application.Quit();
}
}
void LateUpdate () {
this.limitBounds();
}
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "dirt") {
//this.score += 1;
//coll.gameObject.SetActive(false);
}
}
// Check the bounds of the screen
void limitBounds() {
// Keep the object inside screen
float left = Camera.main.ViewportToWorldPoint(Vector3.zero).x;
float right = Camera.main.ViewportToWorldPoint(Vector3.one).x;
float top = Camera.main.ViewportToWorldPoint(Vector3.zero).y;
float bottom = Camera.main.ViewportToWorldPoint(Vector3.one).y;
float x = this.transform.position.x;
float y = this.transform.position.y;
// X axis
if (transform.position.x <= (left + this.spriteRenderer.bounds.extents.x)) {
x = left + this.spriteRenderer.bounds.extents.x;
} else {
if (transform.position.x >= (right - this.spriteRenderer.bounds.extents.x)) {
x = right - this.spriteRenderer.bounds.extents.x;
}
}
// Y axis
if (transform.position.y <= (top + this.spriteRenderer.bounds.extents.y)) {
y = top + this.spriteRenderer.bounds.extents.y;
} else {
if (transform.position.y >= (bottom - this.spriteRenderer.bounds.extents.y)) {
y = bottom - this.spriteRenderer.bounds.extents.y;
}
}
this.transform.position = new Vector3(x, y, this.transform.position.z);
}
}