Hey guys , I have a script attached to a car game object that allows the player to tap the screen and then accelerate , it works fine in Unity Remote , but when I download the APK , as soon as I tap the screen , collisions go completely bezerk and the car just starts ignoring collisions , what’s going on?
The code I think is causing the error starts at line 48
(From main forums , I didn’t get any responses)
using UnityEngine;
using System.Collections;
public class carController : MonoBehaviour {
public float carSpeed;
public float maxPos = 2.2f;
Vector3 position;
public UIManager ui;
public AudioManager am;
public float MaxPosY = 3.69f;
public GameObject anim;
bool currntPlatformAndroid = false;
bool heldDown = true;
Rigidbody2D rb;
void Awake(){
rb = GetComponent<Rigidbody2D> ();
#if UNITY_ANDROID
currntPlatformAndroid = true;
#else
currentPlatformAndroid = false;
#endif
am.carSound.Play ();
}
void Start () {
position = transform.position;
if (currntPlatformAndroid == true) {
Debug.Log ("Android");
}
else {
Debug.Log ("Windows");
}
}
void FixedUpdate() {
if (Input.touchCount > 0)
{
transform.Translate(new Vector3(0, 1, 0) * carSpeed * Time.deltaTime);
}
else {
transform.Translate(new Vector3(0, -1, 0) * carSpeed * Time.deltaTime);
}
}
void Update()
{
if (currntPlatformAndroid == true)
{
//android specific code
//TouchMove ();
AccelerometerMove();
}
else {
position.x += Input.GetAxis("Horizontal") * carSpeed * Time.deltaTime;
position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
transform.position = position;
}
position = transform.position;
position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
transform.position = position;
}
void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.tag == "EnemyCar") {
//Destroy (gameObject);
Instantiate(anim, transform.position, transform.rotation);
am.explosionSound.Play();
Destroy(anim);
Destroy(this.gameObject);
am.carSound.Stop();
ui.gameOverorNot();
}
}
void AccelerometerMove(){
float x = Input.acceleration.x;
Debug.Log ("X = " + x);
if (x < -0.1f) {
MoveLeft ();
} else if (x > 0.1f) {
MoveRight ();
}
else {
SetVelocityZero();
}
}
void TouchMove(){
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch (0);
float middle = Screen.width / 2;
if (touch.position.x < middle && touch.phase == TouchPhase.Began) {
MoveLeft ();
}
else if (touch.position.x > middle && touch.phase == TouchPhase.Began) {
MoveRight ();
}
}
else {
SetVelocityZero();
}
}
public void MoveLeft(){
rb.velocity = new Vector2 (-carSpeed, 0);
}
public void MoveRight(){
rb.velocity = new Vector2 (carSpeed, 0);
}
public void SetVelocityZero(){
rb.velocity = Vector2.zero;
}
/*void AccelerometerMove(){
float x = Input.acceleration.x;
Debug.Log (" X: " + x);
if (x < -0.1f) {
MoveLeft ();
} else if (x > 0.1f) {
MoveRight ();
}
else {
SetVelocityZero();
}
}*/
}
The object that script is attached to has a rigidbody and a box collider , the objects it’s colliding with have box colliders.
EDIT: Just tested again and even if I just use the accelerometer and not touch the screen , the gameobject still goes bezerk and starts ignoring collisions.
My game is 2D