Game Development Community

Top Down Shooter: Errors

by George Fairs · in Torque Game Builder · 02/21/2009 (3:23 pm) · 10 replies

Hey, I have recently been trying to improve my overall "game making" skills, and I have been working on a tiny top down shooter. Basically, I have a top body of my character, and the bottom part. The bottom part is controlled by keys, etc.

The top part is mounted on the bottom part, and is moved around witht he mouse. What I want to do is make it so my bullet sprite is shot out the tip of my pistol (part of my top half character).

But, I use the "Shoots" behaviour, and the bullet gets shot out the side of my character, any way I can make it be where I want?

And also, 2 more things, I have a zombie character that i want to chase my character, using the followObject behviour (I think), but it always stays in the same place (I mean facing the same way, whilst moving towards the player).

And lastly, the player bottom half doesn't aim in the direction I want it to, so the feet are always facing right, even when mving downwards.

If anyone can help me solve these issues, I would much appreciate it, thanks in advance!

#1
02/23/2009 (11:59 am)
Sorry for double posting but nobody seems to have replied, and I really want to get underway with my project, but of course I can't without the help.
#2
02/24/2009 (8:35 am)
Still no replies, anyone? Please I really am in need of help...
#3
02/24/2009 (10:11 am)
Without code, there's not much help I can give you.

As for the behaviour, I've already made my statement on them, but people want to drag and drop in things that they don't fully understand.

Paste the behaviour and maybe I'll look at it. Really, though, you'll have to break it down command by command and learn what they do. Which is the right way to learn, there's a limit to how far one can get by cut and paste for code and editors. You *need* to learn TorqueScript to see how they work and what they're doing.
#4
02/24/2009 (10:33 am)
Thanks Jason, I am learning Torquescript, but for now I am just prototyping with the behaviours...

1. ShooterControls:

//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

if (!isObject(ShooterControlsBehavior))
{
   %template = new BehaviorTemplate(ShooterControlsBehavior);
   
   %template.friendlyName = "Shooter Controls";
   %template.behaviorType = "Input";
   %template.description  = "Shooter style movement control";
   
   %template.addBehaviorField(upKey, "Key to bind to upward movement", keybind, "keyboard up");
   %template.addBehaviorField(downKey, "Key to bind to downward movement", keybind, "keyboard down");
   %template.addBehaviorField(leftKey, "Key to bind to left movement", keybind, "keyboard left");
   %template.addBehaviorField(rightKey, "Key to bind to right movement", keybind, "keyboard right");
   
   %template.addBehaviorField(verticalSpeed, "Speed when moving vertically", float, 20.0);
   %template.addBehaviorField(horizontalSpeed, "Speed when moving horizontally", float, 20.0);
}

function ShooterControlsBehavior::onBehaviorAdd(%this)
{
   if (!isObject(moveMap))
      return;
   
   moveMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), "moveUp", %this);
   moveMap.bindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), "moveDown", %this);
   moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "moveLeft", %this);
   moveMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "moveRight", %this);
   
   %this.up = 0;
   %this.down = 0;
   %this.left = 0;
   %this.right = 0;
}

function ShooterControlsBehavior::onBehaviorRemove(%this)
{
   if (!isObject(moveMap))
      return;

   %this.owner.disableUpdateCallback();
   
   moveMap.unbindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), %this);
   moveMap.unbindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), %this);
   moveMap.unbindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), %this);
   moveMap.unbindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), %this);
   
   %this.up = 0;
   %this.down = 0;
   %this.left = 0;
   %this.right = 0;
}


function ShooterControlsBehavior::updateMovement(%this)
{
   %this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);
   %this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);
}

function ShooterControlsBehavior::moveUp(%this, %val)
{
   %this.up = %val;
   %this.updateMovement();
}

function ShooterControlsBehavior::moveDown(%this, %val)
{
   %this.down = %val;
   %this.updateMovement();
}

function ShooterControlsBehavior::moveLeft(%this, %val)
{
   %this.left = %val;
   %this.updateMovement();
}

function ShooterControlsBehavior::moveRight(%this, %val)
{
   %this.right = %val;
   %this.updateMovement();
}

#5
02/24/2009 (10:34 am)
2. Shoots:
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

if (!isObject(ShootsBehavior))
{
   %template = new BehaviorTemplate(ShootsBehavior);
   
   %template.friendlyName = "Shoots";
   %template.behaviorType = "Game";
   %template.description  = "Shoots an object";

   %template.addBehaviorField(projectile, "The projectile to clone and shoot", object, "", t2dSceneObject);
   %template.addBehaviorField(fireKey, "The key to fire the projectile with", keybind, "keyboard space");
   %template.addBehaviorField(fireRate, "The rate to fire when the fire key is held down (seconds)", float, "0.25");
   %template.addBehaviorField(projectileSpeed, "The speed of projectiles (world units per second)", float, "150");
}

function ShootsBehavior::onBehaviorAdd(%this)
{
   if (isObject(moveMap))
      moveMap.bindObj(getWord(%this.fireKey, 0), getWord(%this.fireKey, 1), "fire", %this);
}

function ShootsBehavior::onBehaviorRemove(%this)
{
   if (isObject(moveMap))
      moveMap.unbindObj(getWord(%this.fireKey, 0), getWord(%this.fireKey, 1), %this);
}

function ShootsBehavior::fire(%this, %val)
{
   if (%val == 0)
   {
      cancel(%this.fireSchedule);
      return;
   }
   
   if (!isObject(%this.projectile))
      return;
   
   %projectile = %this.projectile.cloneWithBehaviors();
   
   %projectile.setPosition(%this.owner.position);
   %projectile.setRotation(%this.owner.rotation);
   %projectile.setLinearVelocityPolar(%this.owner.rotation, %this.projectileSpeed);
   
   if (!isEventPending(%this.fireSchedule))
      %this.fireSchedule = %this.schedule(%this.fireRate * 1000, "fire", 1);
}
#6
02/24/2009 (11:04 am)
function ShooterControlsBehavior::moveDown(%this, %val)
{
   %this.down = %val;
   %this.updateMovement();
}

Nothing in this changes you rotation of your player. Which leads into the other problem.

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

This is your shooting issue. It starts at your the calling objects position and uses your object's rotation twice. If your object never changes rotation, which is highly distinct from velocity, you'll never fire any other direction.

Play around with setRotation and then do your firing to see what I mean.
#7
02/24/2009 (11:11 am)
Ahh I see, thanks...
#8
02/25/2009 (7:45 am)
Did you fix your issue?
#9
02/25/2009 (8:17 am)
Well, I took a look at your scripts, and I can't think of how I can manipulate my behaviours to work... Oh and I took your advice and I made a walking controls without a bheviour, but it doesn't work?

function PlayerBottom:: onLevelLoaded(%this, %scenegraph)
{
	moveMap.bindCmd(keyboard, "w", "playerBottomUp();", "playerBottomUpStop();");
	moveMap.bindCmd(keyboard, "a", "playerBottomLeft();", "playerBottomLeftStop();");
	moveMap.bindCmd(keyboard, "s", "playerBottomDown();", "playerBottomDownStop();");
	moveMap.bindCmd(keyboard, "d", "playerBottomRight();", "playerBottomRightStop();");
}

function playerBottomUp()
{
	$PlayerBottom.setLinearVelocityY( -20 );
}

function playerBottomLeft()
{
	$PlayerBottom.setLinearVelocityX( -20 );
}

function playerBottomDown()
{
	$PlayerBottom.setLinearVelocityY( 20 );
}

function playerBottomRight()
{
	$PlayerBottom.setLinearVelocityX( 20 );
}




function playerBottomUpStop()
{
	$PlayerBottom.setLinearVelocityY( 0 );
}

function playerBottomLeftStop()
{
	$PlayerBottom.setLinearVelocityX( 0 );
}

function playerBottomDownStop()
{
	$PlayerBottom.setLinearVelocityY( 0 );
}

function playerBottomRightStop()
{
	$PlayerBottom.setLinearVelocityX( 0 );
}
#10
02/25/2009 (8:38 am)
George, don't let your enthusiasm get the better of you. Doing it without a behaviour in a hurry isn't going to help. I would recommend you put that behaviour back until you figure out your shooting business. One thing at a time.