Hey guys! Currently working on a game I need to make and am currently running into two issues:
-
This one is probably more simple. My player has two attack animations that he should alternate between if the button is pressed. However if the button is not pressed within a short time it will reset to the first attack. I know I probably need a stopwatch but i’m not sure how to do it. I really just need some instructions on how to do that.
-
My character can jump and land on my tilemap but only if there are two solid grid places taken up. But my tilemap also has like wooden platforms that do not take up a full grid box. Anyone might know whats wrong?
- Lots of ways you can do this. For the reset portion you can add in some FixedUpdate code (could do it in update too, but fixedupdate should be fine) Just have a variable with the total time passed since the last attack and fixedupdate increases the value of such by Time.fixeddeltatime. If that total exceeds your reset time, reset the animation and stop tracking the time as it isn’t necessary. While not the same, here is my mob’s aggro mechanic that does this sort of thing:
void FixedUpdate()
{
RigidBody2D_var.MovePosition(RigidBody2D_var.position + moveVelocity * Time.fixedDeltaTime);
if (MobBehavior == Behavior.Hostile || MobBehavior == Behavior.Timid)
{
if (playerSeen || Vector3.Distance(this.transform.position, PlayerData.Current.PlayerPosition) < GameConstants.AGGRORESETDISTANCE)
{
Vector3 toPlayer = PlayerData.Current.PlayerPosition - this.transform.position;
playerSeen = true;
if (MobBehavior == Behavior.Hostile)
{
moveVelocity = new Vector2(toPlayer.x, toPlayer.y).normalized * Speed;
}
else // timid
{
moveVelocity = new Vector2(-toPlayer.x, -toPlayer.y).normalized * Speed;
}
}
}
if (timeSinceLastAIUpdate >= ActionIntervalTime && !playerSeen)
{
if (MobBehavior == Behavior.Idle)
{
// do nothing
}
else if (MobBehavior == Behavior.Passive || !playerSeen)
{
// idle around
if (moveVelocity.magnitude == 0)
{
// mob is stopped, so choose a random direction and move for a bit
moveVelocity = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)).normalized * Speed;
}
else
{
// mob is moving, so stop and idle for a bit
moveVelocity = new Vector2(0, 0);
}
}
timeSinceLastAIUpdate = 0;
}
else
timeSinceLastAIUpdate += Time.fixedDeltaTime;
if (playerSeen)
{
timeSincePlayerSeen += Time.fixedDeltaTime;
if (timeSincePlayerSeen > GameConstants.AGGRORESETTIME)
{
timeSincePlayerSeen = 0;
playerSeen = false;
}
}
if (MobData_var != null)
{
MobData_var.Position = CustomUtilities.WorldToGamePosition(this.transform.position);
MobData_var.Orientation = lastMove;
MobData_var.CurrentVelocity = moveVelocity;
MobData_var.LastAIUpdate = timeSinceLastAIUpdate;
}
}
- tilemap tiles are by default 1 game unit in size. So, if your tile sprite is not filling that, it means its import settings make it less than 1 unit in size. This typically means your pixels per unit on the sprite is set wrong. For instance if you have a 32x32 tile and it is at the default of 100 pixels per unit, it will only fill about 1/3 of the tilespace vertically/horizontally. You’d want the pixels per unit to match the tile at 32.