Chopping repeat action for sword with use ability

Silver100

Member
Thought this could be useful for chopping action. Just been figuring use ability and boolions.

Code:
using UnityEngine;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Character.Abilities.Items;
using System.Collections;// must be included
public class Choppingaction : MonoBehaviour//
{

[Tooltip("A reference to the Ultimate Character Controller character.")]
[SerializeField] protected GameObject m_Character;

bool SwordEquipped;

/// <summary>
/// Equips the item.
/// </summary>
///
private void Update()
{

if (Input.GetKeyDown(KeyCode.J) && SwordEquipped)// or what ever key or button you like
{
var characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>();
if (characterLocomotion != null)
{
// Equip a specific index within the ItemSetManager with the EquipUnequip ability.
var use = characterLocomotion.GetAbility<Use>();
if (use != null)
{

StartCoroutine(delay(v: 30));
}
IEnumerator delay(int v)

{
characterLocomotion.TryStartAbility(characterLocomotion.GetAbility<Use>());// 1
yield return new WaitForSeconds(0.4f);//
characterLocomotion.TryStopAbility(characterLocomotion.GetAbility<Use>());// Pause
yield return new WaitForSeconds(0.4f);//
characterLocomotion.TryStartAbility(characterLocomotion.GetAbility<Use>());// 2
yield return new WaitForSeconds(0.4f);
characterLocomotion.TryStopAbility(characterLocomotion.GetAbility<Use>());// Pause
yield return new WaitForSeconds(0.4f);//
characterLocomotion.TryStartAbility(characterLocomotion.GetAbility<Use>());// 3
yield return new WaitForSeconds(0.4f);//
characterLocomotion.TryStopAbility(characterLocomotion.GetAbility<Use>());// Pause
yield return new WaitForSeconds(0.4f);//


}

}
}
}

public void OnTriggerEnter(Collider other)// use a tag on the sword (here I have named (swordequip lower case) in unity// may be another better way to do this, I'm just learning
{
if (other.tag == "swordequip") // Named however you want but must match the tag located near inspector on each object here I have tagged the players sword as swordequip, so when player has the sword in hand its detected through collision.
{
SwordEquipped = true;// Boolion used here. When players hand makes contact with the sword bool is now set true
}
else
{
SwordEquipped = false;// if sword is not is hand the bool is now set false
}

}
}
Code:
 
Last edited:
I'm learning how to use UCC also, and this ability looks cool so i did a couple untested optimizations.
Took the IEnumerator out of the Update method, and used a while loop, also declared the UCC component as a class variable.
using UnityEngine; using Opsive.UltimateCharacterController.Character; using Opsive.UltimateCharacterController.Character.Abilities.Items; using System.Collections;// must be included public class Choppingaction : MonoBehaviour// { [Tooltip("A reference to the Ultimate Character Controller character.")] [SerializeField] protected GameObject m_Character; bool chopping = false; bool SwordEquipped; UltimateCharacterLocomotion characterLocomotion; /// <summary> /// Equips the item. /// </summary> /// private void Awake() { characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>(); if (!characterLocomotion) Debug.Log("No UltimateCharacterLocomotion Found!" + name); } private void Update() { if (Input.GetKeyUp(KeyCode.J) && SwordEquipped)// or what ever key or button you like { if (characterLocomotion != null && !chopping) { StartCoroutine("ChopDelay"); } } } IEnumerator ChopDelay() { bool pause = true; int pauses = 6; int steps = 1; while (pause == true) { characterLocomotion.TryStartAbility(characterLocomotion.GetAbility<Use>()); steps++; if (steps == pauses) pause = false; yield return new WaitForSeconds(0.4f); } chopping = false; } public void OnTriggerEnter(Collider other)// use a tag on the sword (here I have named (swordequip lower case) in unity // may be another better way to do this, I'm just learning { SwordEquipped = other.tag == "swordequip" ? true : false; } }
*Edited code, added bool check to prevent multiple starts of ChopDelay, GetKeyUp to insure only triggered once.
i will test it out later when finished work and revise if necessary.
 
Last edited:
Thanks that looks a much better method with what your doing there. I look forward to your results. I think another way is to extend & duplicate the animation, following the add new ability tutorial shows you how to add new ability like crawl (but apply a chopping animation) it's not nearly as tricky as it looks but you have to really follow the steps carefully. I'm almost sure this is the best way forward long term for this situation.


This can be any animation Ideally even getting hold of a tree chop style animation too / or duplicating the sword animation a few times over in a time line.

I adapted & added a really basic animation character lifts arms on impact force through watching this.

However for initial speedy results I think that IEnumerator method is probably a good way too.
 
Last edited:
Top