Question by
SteveJBruce · Sep 11, 2016 at 02:49 AM ·
collider2d2d rotation
BoxCollider2D rotation not the same as GameObject rotation
Hi all,
I'm having a bit of an issue with a BoxCollider2D I have on my GameObject. When I rotate the GameObject, the BoxCollider2D rotates with it, but not as fast. Is there a way to get the BoxCollider2D to move at the same rate as the GameObject? I feel like I'm missing something obvious.
Before rotation:
After rotation:
Below is my code for the movement of the player:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
Animator anim;
Rigidbody2D rbody;
float speed = 0f;
public float moveSpeed = 0.6f;
public float acceleration = 0.2f;
public int turnSpeed = 20;
bool sails = false;
// Use this for initialization
void Start () {
anim = GetComponentInChildren<Animator> ();
rbody = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
if (sails) {
rbody.transform.Translate (transform.right * (speed * Time.deltaTime));
speed += acceleration * Time.deltaTime;
if (speed > moveSpeed)
speed = moveSpeed;
if (Input.GetKey (KeyCode.LeftArrow)) {
rbody.transform.Rotate (0,0,turnSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.RightArrow)) {
rbody.transform.Rotate (0,0,-turnSpeed * Time.deltaTime);
}
}
if (!sails) {
rbody.transform.Translate (transform.right * (speed * Time.deltaTime));
speed += -acceleration * Time.deltaTime;
if (speed < 0f)
speed = 0f;
}
if (Input.GetKeyDown (KeyCode.Space)) {
sails = !sails;
anim.SetBool ("sailsDown", sails);
}
}
}
shipboxcolliderbefore.png
(68.7 kB)
shipboxcolliderafter.png
(82.8 kB)
Comment
Best Answer
Answer by SteveJBruce · Sep 11, 2016 at 11:50 AM
For anybody that has this issue as well, it has been answered over on StackOverflow
http://stackoverflow.com/questions/39434831/unity-boxcollider2d-rotation-not-the-same-as-gameobject-rotation