Here is the code from my combat mod that changes move speed based on sheath/attack state
Code: Select all
//sets players current movement using overrides and override float values. Ensures triggered only once when setting values to save update cycles.
void movementModifier()
{
//the player isn't sheathed, idle, and restoredWalk hasn't been triggered by sheathing yet. This is default movement speed unsheathed modifier.
if(!GameManager.Instance.WeaponManager.Sheathed && AttackState == 0 && !restoredWalk)
{
walkspeed = playerSpeedChanger.GetWalkSpeed(GameManager.Instance.PlayerEntity);
playerSpeedChanger.StoreWalkSpeed();
playerSpeedChanger.SetWalkSpeed(walkspeed * .45f, true);
restoredWalk = true;
attackApplied = false;
return;
}
//if the player is attacking, is unsheathed, and hasn't already had movement modifier attackApplied, set walkspeed based.
if (AttackState != 0 && !GameManager.Instance.WeaponManager.Sheathed && restoredWalk && !attackApplied)
{
walkspeed = playerSpeedChanger.GetWalkSpeed(GameManager.Instance.PlayerEntity);
playerSpeedChanger.StoreWalkSpeed();
playerSpeedChanger.SetWalkSpeed(walkspeed * .01f, true);
attackApplied = true;
return;
}
//if weapon is sheathed and restore walk has been set to true, restore sheathed movement speed using the last walk speed value.
if (GameManager.Instance.WeaponManager.Sheathed && restoredWalk)
{
restoredWalk = playerSpeedChanger.RestoreWalkSpeed();
restoredWalk = false;
attackApplied = false;
Debug.Log("restore walkspeed");
return;
}
Code: Select all
// Update is called once per frame
void Update()
{
if (OnTileChange())
{
List<float> TileProperties = TileProperty(lastTile);
movementModifier(TileProperties[0]);
Debug.Log("New Ground Tile: " + lastTile.ToString() + " | " + TileProperties[0].ToString());
}
}
void movementModifier(float moveModifier)
{
//on tile change, update the current speed using an additive model to ensure it modifies based on any previous number being used.
playerSpeedChanger.SetWalkSpeed(playerSpeedChanger.GetWalkSpeed(GameManager.Instance.PlayerEntity) * moveModifier, true);
//store the new value so it can be restored and used when computed in future mod/code execution
playerSpeedChanger.StoreWalkSpeed();
}