Search Unity

Math problem, projectile accuracy changed with movement of the player

Discussion in 'Scripting' started by BooBi, Jan 15, 2014.

  1. BooBi

    BooBi

    Joined:
    Jan 18, 2010
    Posts:
    534
    Hi,

    I'm having a little problem here.

    I'm doing a first person view space shooter. The player's ship is on an orbit around the ennemies (only on the XZ plan (rotate around the Y axis), so on a circular orbit not a spherical) and it can pan up and down from his position on the orbit.
    $Orbit.jpg
    The ship keep firing as you move along with your finger (it's a one finger control system). For the rotation and the panning I've used the Fingergesture scripts.

    What I'm trying to achieve now is a sort of inaccuracy factor when moving while shooting. Basically the aim gets less accurate with your speed.
    My idea toward that is to do a circle on a perpendicular plan of the actual shooting axis and randomise the vector of the shot from the base of the ship gun to a point in that circle please see attached the shooting.jpg it will make more sense.
    $Shooting.jpg

    At the moment I'm using this code to shoot:
    Code (csharp):
    1.  
    2. void Fire()
    3.     {
    4.         if(Input.GetButton("Fire1")  Time.time > nextFire)
    5.         {
    6.             nextFire = Time.time + fireRate;
    7.             ForceFire();   
    8.         }
    9.     }
    10.     void ForceFire()
    11.     {
    12.         GameObject thebullet = Instantiate(Bullet, Shooter.transform.position,this.transform.rotation) as GameObject;
    13.         thebullet.rigidbody.AddForce(this.transform.forward*bulletImpulse,ForceMode.Impulse);                                    
    14.     }
    15.  
    Basically I need to create a vector or quaternion of inaccuracy to add to the this.transfrom.rotation.

    I really don't get how to manage the two vector (intitial rotation and inaccuracy) and how to link the movement to the inaccuracy. If possible, I would like to make the circle of inaccuracy grow or shrink depending of the speed.

    My idea would be to use a random range for the rotation in Z (up/down) and Y(left/right) the speed of the movement would be linked (idk how) with the random range max number (0 being no movement 10° being highest speed).

    Questions:

    - How can I determine how fast the ship is moving (using the script TBOrbit and TBPan from FingerGesture)?

    - Once I've got my random ranged rotation: vector3(x, random.range(0,SpeedCoef),random.range(0,SpeedCoef)) how can I apply it to my quaternion rotation?

    - Is calculating that constanly won't kill the performances? is there a better way to do that?


    Thanks a lot for your help.