- Home /
Unity 2D rotation speeds problem
I have a simple character which has another object attached to him called arm. Arm rotates arround using this script:
using UnityEngine;
using System.Collections;
public class playerArmScript : MonoBehaviour {
public int zrotate = 1;//have tried using float as well but that made it so that zrotate
became 10 when i Debug.Log'ed it and the other zrotates to still be 1
// Use this for initialization
void Start () {
Debug.Log ("zrotateArm:");
Debug.Log (zrotate);
}
// Update is called once per frame
void FixedUpdate () {
transform.Rotate (0, 0, zrotate);
}
}
I have a hand object attached to arm, that has a sword sprite, it rotates just fine along with the arm.
The problem lies in the fact that I want the arm and sword to have colliders, so my arm has a box collider 2d and my sword does as well, These colliders are seperate objects attached to the arm and the sword (hand).
They have the following scripts attached to them (I made seperate scripts to be able to have them both interact with colliding object in another way in the future):
using UnityEngine;
using System.Collections;
public class playerHandCollider : MonoBehaviour {
playerArmScript armscrip;
public int zrotatehandcol;
// Use this for initialization
void Start () {
armscrip = new playerArmScript ();
zrotatehandcol = armscrip.zrotate;
}
// Update is called once per frame
void FixedUpdate () {
transform.Rotate (0, 0, zrotatehandcol);
}
}
using UnityEngine;
using System.Collections;
public class playerArmCollider : MonoBehaviour {
playerArmScript armscrip;
public int zrotatearmcol;
// Use this for initialization
void Start () {
armscrip = new playerArmScript ();
zrotatearmcol = armscrip.zrotate;
Debug.Log ("zrotateArmCol:");
Debug.Log (zrotatearmcol);
}
// Update is called once per frame
void FixedUpdate () {
transform.Rotate (0, 0,zrotatearmcol);
}
}
I know using new to initialise an object of playerArmScript isn't the good way of doing it because it's a monobehaviour extending class, but it's the only way I don't get the following error: "NullReferenceException: Object reference not set to an instance of an object" (this happens whenever I use GetComponent();)
hope someone can help me out :)
Answer by jurgjurg · Nov 16, 2014 at 07:16 PM
I fixed it! now using the following scripts:
using UnityEngine;
using System.Collections;
public class playerArmScript : MonoBehaviour {
public int zrotate = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
transform.Rotate (0, 0, zrotate);
}
}
using UnityEngine;
using System.Collections;
public class playerArmCollider : MonoBehaviour {
public int zrotatearmcol;
GameObject arm;
public float armrotZ;
// Use this for initialization
void Start () {
arm = GameObject.Find("playerArm");
}
// Update is called once per frame
void FixedUpdate () {
armrotZ = arm.transform.rotation.eulerAngles.z;
Debug.Log (armrotZ);
gameObject.transform.eulerAngles = new Vector3(0,0, armrotZ);
}
}
Flipping my character model stil makes it spass out :(