Game Development Community

Cant get Aimer/Gun to rotate with player, Help Please

by Anachronistic · in Torque X Platformer Kit · 08/04/2010 (2:07 am) · 8 replies

As it stands at the moment, im stuck with a niggling issue that is rather annoying. I have completed and integrated the "TorqueX/PlatformerFrameworkShooter" from the TDN, and its doing what it professes to do, the problem that im having now is as follows.

Im creating a 2D platformer/shooter, the above shooting code works well for firing the players weapon to the right, however when the player turns to face left, the linked objects do not flip with the player, creating a situation where the player fires right when he should be firing left. I have tried turning collision off on the player to somewhat solve my problem, but this turns the platformer into a faller for obvious reasons.

So my question is this, how can i flip the two attached components (aimer and gun) when the player sprite flips?


the tute/code for the shooter can be found at:

http://tdn.garagegames.com/wiki/TorqueX/PlatformerFrameworkShooter


#1
08/04/2010 (5:19 pm)
Try to make sure that any objects that need to inherit the flip have the InheritMountFlip property set equal to true.

If it doesn't flip fast enough, you can call "SnapToMount()" on the scene object as well.
#2
08/04/2010 (6:29 pm)
Hey there,

Got your email sorry for the slow reply. Looks like Will has already answered! I do however have some old mouse aiming code lying around if that would be of any use.

Henry
#3
08/05/2010 (2:04 am)
geez i knew it was simple thankyou very much. I was attempting to turn that on through code all day but was not aware that a check box existed.

#4
08/06/2010 (12:59 am)
Thanks again for the quick responses guys,

So ive got the mounted objects inheriting the flip of the player, but now ive run into two problems.

1 - When flipped, if i use the right thumb stick to move the aimer, it instantly flips back over to the right side of the character (character stays facing left). Also associated with this is the problem that when i can get the aimer to move on the left facing side, the axis for the aimer movement are inverted. Im thinking that im going to have to edit the _AimMove() function in some way, but i have no idea where to start.

2 - When facing left with the aimer on the left, the projectile spawns from the flipped gun point, but still moves to the right. Agiain im thinking that im going to have to edit the code in some way to get this working properly ie face left fire left, face right fire right.

Im new to this, and i have bitten off more than i can chew, got to have a functionaly prototype going by the 20th. Any help will be greatly appreciated, thanks in advance.

Anachronistic
#5
08/06/2010 (1:56 am)
Update.

Ive figured that if you change the sign of the multiplyer in the

" projectileObj.Physics.Velocity" line the player shoots to the left.

Now i need to work out how to change the sign of the multiplyer based on if InheritMountFlip is true or false. well thats what im assuming will work.
#6
08/06/2010 (4:07 am)
Update 2.

So i have something somewhat working, its still very buggy, hopefully someone can help me clean it up and get it more functional.

Ive edited _Fire() as below
protected void _Fire()
        {
      



            T2DSceneObject gun = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("gun");
            T2DSceneObject player = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Player");
            T2DSceneObject projectileObj = TorqueObjectDatabase.Instance.CloneObject<T2DSceneObject>("ProjectileTemplate");
            T2DSceneObject aimer = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("aimer");
           

            projectileObj.Position = gun.Position; // Set the "bullet" to "spawn" from where the gun is positiond
            projectileObj.Rotation = aimer.Rotation -100; // Set the "bullet" to be facing the right way 
            projectileObj.Layer = gun.Layer + 1;
            projectileObj.Physics.Velocity = T2DVectorUtil.VectorFromAngle(aimer.Rotation + 90) * ProjectileSpeed; // Turns the rotation of the aimer into a vector to use as velocity
            {
                if (TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Player").FlipX == true)
                {
                    ProjectileSpeed = -ProjectileSpeed;
                    TorqueObjectDatabase.Instance.Register(projectileObj);
                }
                else
                {
                    TorqueObjectDatabase.Instance.Register(projectileObj);
                }
            }
           
        }

Sometimes it wont fire in the direction the player is facing, and you have to flip back then back again to get it to work.

Going to work on a similar fix for the aimer axis inversion.

Any help cleaning up the functionality of the code would be greatly appreciated

#7
08/06/2010 (4:49 am)
Well I've had a reasonable productive day today,

Ive sorted the Aimer movement problem

protected void _AimMove(Vector2 v)
        {
            T2DSceneObject aimer = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("aimer");
            //aimer.Rotation = T2DVectorUtil.AngleFromInput(v) - 90; - Original shooter code.
            {
                if (TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Player").FlipX == true)
                {
                    aimer.Rotation = (T2DVectorUtil.AngleFromInput(v) - 270);
                }
                else
                {
                    aimer.Rotation = T2DVectorUtil.AngleFromInput(v) - 90;
                }
            }
        }


Still getting lots of buggy firing with the _Fire() code section. Any help would be great.
#8
08/10/2010 (1:03 am)
Alright,

Persistance payed off. This all works

There are Three new declared values. These need to be added to the
" #region Private, protected, internal fields" section of PlayerContoller.cs

// Set up Variables for the speed of the Projectile
        private int ProjectileSpeed;
        // Set Movement speed to the right
        private int ProjectileSpeedRight = 80;
        // Set Movement speed to the left
        private int ProjectileSpeedLeft = -80;

Also in PlayerController.cs change the following two functions to look as follows.

protected void _Fire()
        {
            T2DSceneObject gun = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("gun");
            T2DSceneObject player = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Player");
            T2DSceneObject projectileObj = TorqueObjectDatabase.Instance.CloneObject<T2DSceneObject>("ProjectileTemplate");
            T2DSceneObject aimer = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("aimer");

            //Set up values for the Instantiation of the projectile.
            projectileObj.Position = gun.Position; // Set the "bullet" to "spawn" from where the gun is positiond
            projectileObj.Rotation = aimer.Rotation - 100; // Set the "bullet" to be facing the right way 
            projectileObj.Layer = gun.Layer + 1;
            {// Test to see if the "Player" is flipped or not, 
                if (TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Player").FlipX == true)
                {
                    ProjectileSpeed = ProjectileSpeedLeft;
                    projectileObj.Physics.Velocity = T2DVectorUtil.VectorFromAngle(aimer.Rotation + 90) * ProjectileSpeed;// Turns the rotation of the aimer into a vector to use as velocity in the left direction
                    TorqueObjectDatabase.Instance.Register(projectileObj);// creates the projectile object, with the properties set up above.                    
                }
                else
                {
                    ProjectileSpeed = ProjectileSpeedRight;
                    projectileObj.Physics.Velocity = T2DVectorUtil.VectorFromAngle(aimer.Rotation + 90) * ProjectileSpeed;// Turns the rotation of the aimer into a vector to use as velocity in the right direction
                    TorqueObjectDatabase.Instance.Register(projectileObj);// creates the projectile object, with the properties set up above.
                }
            }         
        }

and

protected void _AimMove(Vector2 v)// Aimer Move, with check for the player flip.
        {
            T2DSceneObject aimer = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("aimer");

            if (TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Player").FlipX == true)
            {
                aimer.Rotation = (T2DVectorUtil.AngleFromInput(v) - 270);
            }
            else
            {
                aimer.Rotation = T2DVectorUtil.AngleFromInput(v) - 90;
            }
        }

This is all edits of the code that Henry put up on the wiki at
http://tdn.garagegames.com/wiki/TorqueX/PlatformerFrameworkShooter for those that are reading this thread without having implemented the code in his tutorial.

Hoping that this may be of help to others.