Game Development Community

Help with shoots behavior

by Ryan Jones · in Torque Game Builder · 12/17/2008 (4:43 pm) · 2 replies

I'm experimenting with a Contra styled game where the player can shoot at various angles and I decided the best way to go about this would be the shoots behavior.

Shoots behavior by default shoots directly up like it's suppose to, but I'd prefer to have it shoot at various angles. There are two things which i'm trying to figure out how to manipulate this code.

Let's assume the player's sprite is as follows:
Object name: player
Class: PlayerClass

%projectile = %this.projectile.cloneWithBehaviors();

%projectile.setPosition(%this.owner.position);
%projectile.setRotation(%this.owner.rotation);
%projectile.setLinearVelocityPolar(%this.owner.rotation, %this.projectileSpeed);

#1 I'd like to be able to offset the start position of the shot for my game so the shooting point begins at the end of a gun. I've tried a few things but the starting position of the shot comes from the center of my character. For example I tried this without success:
%projectile.setPositionX(%this.owner.getPositionX() + 1);
%projectile.setPositionY(%this.owner.getPositionY() + 1);

#2 I'd like to be able to change the direction of shot besides directly up. Once again, I tried various ways to manipulate this code but without success. I mean to me if I change %this.owner.rotation to a number say 45 then I would think something should change.

#1
12/26/2008 (7:49 pm)
Hi Ryan, I hope I can offer some help. Correct me if I misunderstand!

For a Contra style game, I would expect the player to shoot left when the joystick is pressed left and the fire button is pressed. I would expect the player to shoot up and left if the joystick is pushed up and left and the fire button is pressed.

In your joystick input I would expect you to detect a left fire and call FireLeft() and a left/up fire and call FireLeftUp(). In FireLeft() I would expect that you wouldn't care about the rotation of the player, you would simply set a linear velocity in -x on the projectile. In FireLeftUp() you would not care about the rotation of the player either. You could set a linear velocity on the projectile in -x and in -y OR you could set the correct angle on the Polar Velocity (what would it be? -45 degrees for up and left? I am new to torque).

As for the position, again I am new to torque. I suspect your current script works and the "+1" being added to the position is too small of an offset to notice (try +100), the .exe never calls that line of script (try setting a breakpoint on it and see if it hits that line), or other script is overriding it (again, watch the position value and use breakpoints).

Let me know how you make out!

Cheers,
Jim
#2
01/06/2009 (6:35 pm)
You were correct about the position, +1 was just so minute I didn't notice a difference. When I did

%projectile.setPositionX(%this.owner.getPositionX() + 10);
%projectile.setPositionY(%this.owner.getPositionY() + 10);

I noticed a big difference so I'm glad that worked.

Also I experimented with using this:
%projectile.setLinearVelocityPolar(90, 150);

And it works great, so thanks again for the inspiration to figure this out.