- Home /
How to solve weird rotation problem?
Hey, I'm just making a weapon switching script, I keep both my weapon as children of the main camera, but for some weird reason, as I walk or jump, the graphics inside the gun gameObject rotate and I can no longer see them. This does not happen when I switch off my script. To solve it right now I got a lateUpdate function placing their local rotation and position to 0, but would like to fix it the right way. Here is what I have of the script. P.S. I've also tried searching for a switching script on the web, but the same thing happens.
var maxGuns : int = 4;
var guns : GameObject[];
@HideInInspector
var playerMovementScript : PlayerMovement;
var activeGun : int = 0;
var player : Transform;
function activateGun (gun : int){
playerMovementScript.currentGun.GetComponent(GunScriptNoSD).beingHeld = false;
playerMovementScript.currentGun.gameObject.SetActiveRecursively(false);
guns[gun].SetActiveRecursively(true);
guns[gun].GetComponent(GunScriptNoSD).beingHeld = true;
playerMovementScript.currentGun = guns[gun];
guns[gun].transform.localPosition = Vector3(0,0,0);
}
function Start () {
playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovement);
player = GameObject.FindWithTag("Player").transform;
playerMovementScript.currentGun.GetComponent(GunScriptNoSD).beingHeld = false;
for (var i = 0; i < guns.length; i++){
print (guns[i].name);
guns[i].SetActiveRecursively(false);
guns[i].transform.localPosition = Vector3(0,0,0);
}
playerMovementScript.currentGun = guns[0];
guns[0].SetActiveRecursively(true);
guns[0].transform.localPosition = Vector3(0,0,0);
guns[0].GetComponent(GunScriptNoSD).beingHeld = true;
activeGun = 0;
}
function Update () {
if (Input.GetButtonDown("Switch")){
activeGun += 1;
if (activeGun > guns.Length - 1){
activeGun = 0;
}
activateGun (activeGun);
}
}
Answer by darkcookie · Apr 15, 2012 at 01:50 AM
check if all the axes(X,Y,Z) are the same for the gun and every object that you have....also center the orgins...
Your answer
Follow this Question
Related Questions
"Object reference not set to an instance of an Object" 0 Answers
Prefab Character position/rotation resets 0 Answers
How can I change rotation of parent independently without affecting angles and rotation of child ?? 2 Answers
Precise rotation based on joystick axis input for twin-stick controls 3 Answers