- Home /
Unity colliders bug (2d box)
https://dl.dropboxusercontent.com/u/151460612/bug/bug.swf.html I recorded a little video to show you this bug, as you can see from the video, I have an empty object that works as pivot, and it's child should be a sprite, or even a sprite with collider, for the video I shown both, and same have same issue, when the object should look at the left side of the screen, it gets flipped(the z axis poinst to the other) and then the collider get's all messed up, how can I fix this issue? I started this project today and this is kinda pissing me off :/ I only have one script yet
using System.Collections;
public class KatanaScript : MonoBehaviour {
public Transform katanaTarget;
static bool katanaIsActive = false;
public bool isTarget;
public float rotateVelocity;
void Update () {
if (isTarget) {
int number = 0;
if (Input.GetMouseButton(0)) {
number++;
}
if (number >= 1) {
katanaTarget.gameObject.SetActive(true);
Vector2 pos = Input.mousePosition;
pos = Camera.main.ScreenToWorldPoint(new Vector2(pos.x, pos.y));
katanaTarget.position = pos;
katanaIsActive = true;
}
else {
katanaTarget.gameObject.SetActive(false);
katanaIsActive = false;
}
}
else {
if(katanaIsActive) {
transform.LookAt(katanaTarget.position);
transform.Rotate( new Vector3(0, -90, 0));
}
}
}
}
also, when the object is totally point up, it rotates 90º on the y axis, and when it gets over that (starts pointing at the left side) it rotates another more 90º.
Answer by MadJohny · Apr 02, 2014 at 02:37 PM
I've got this to work, the problem is that my script was rotating on all axis, causing weird behaviours for a 2d game, this is what I used instead:
Vector3 vectorToTarget = katanaTarget.position - transform.position;
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rotationVelocity);
Answer by Abdullah-Alaskari · Apr 02, 2014 at 02:21 PM
Hi There, Try to use rigidbody2d, as far as i know these are the only thing that can be rotated in 2d without bugging the collider.
Static RigidBody2d is good i think, maybe addTorque.
I'll answer my own question because I fixed it before any answers