Rotation Problem (20886)

Here's the Script:

var CloneObject : Transform;

function Update () {

var Center = CenterObj.position;

    if (Input.GetKey("right")){
        transform.RotateAround(Center, Vector3.up, 25 * Time.deltaTime);
    }

    if (Input.GetKey("left")){
        transform.RotateAround(Center, Vector3.up, -25 * Time.deltaTime);
    }

    if (Input.GetKey("up")){
        transform.RotateAround(Center, Vector3.right, 25 * Time.deltaTime);
    }

    if (Input.GetKey("down")){
        transform.RotateAround(Center, Vector3.right, -25 * Time.deltaTime);
    }
Shoot();
}

function Shoot(){
    var hit : RaycastHit;
    var rndRotate =transform.TransformDirection(Vector3(Random.Range(-0.1, 0.1), Random.Range(-0.1, 0.1), -1));
        if(Physics.Raycast (transform.TransformPoint(0, 0.07, -1.2), rndRotate, hit, 200)){
            Debug.DrawLine (transform.TransformPoint(0, 0.07, -1.2), hit.point, Color.green);
                var hitRotation = Quaternion.FromToRotation(Vector3(0, 1, 0), hit.normal);

                var bullet : Transform;
                bullet = Instantiate(Bullet, transform.TransformPoint(0, 0.07, -1.2), transform.localRotation);//InverseTransformDirection(0, 0, 1)
                bullet.Rotate(rndRotate*70);
                bullet.parent = CloneObject;
        }
    }
}

The Problem i am having is that when the gun rotates to the left, the bullets begin shooting downwards, and when i move it to the right, they begin shooting upwards.

Here's the script to move the bullets:

var BulletSpeed : int = 50;

function Start(){
    rigidbody.AddRelativeForce(Vector3(0, 0, 1) * BulletSpeed);
}

Anyone know why the Y rotation of my gun has somehow been changed to the X rotation of my bullets? Because the bullets shoot straight forward when the gun is straight forward, and only the X rotation of the bullets change when the Y rotation of the gun is changed.

What is ClonedObject?

Oh, Clone object is just there to hide all the clones in my hierarchy, its an empty gameobject.

1 Answer

1

I just did it without bones. It works now.

@jaypordy It will use more processing, but if this will cause issues it depends on the use-case and the number of objects that are being included in the calculation. You can always test it and use the monitoring tools to see the performance impact on the game.