Search Unity

2D Script for arm chopping motion

Discussion in 'Scripting' started by bsullivan28, Jul 25, 2021.

  1. bsullivan28

    bsullivan28

    Joined:
    Jul 20, 2021
    Posts:
    2
    I want to make a 2D arm that starts centered, then comes up and chops down, then returns to center. This action would be triggered by a button push.

    Basically a Karate chop motion.

    It would rotate at the shoulder essentially, no elbow or wrist bend needed.

    No animation needed.

    I am struggling to find script examples for something like this, if anyone can help that would be great.

    thanks!
     
  2. TheFunnySide

    TheFunnySide

    Joined:
    Nov 17, 2018
    Posts:
    200
    You absolutely do not want to hardcode animations.

    If you really want to do it thought.
    Make sure the center of your arm is at the shoulder
    Then overtime change its transform.rotation
    Look up how Coroutines work.
     
  3. bsullivan28

    bsullivan28

    Joined:
    Jul 20, 2021
    Posts:
    2
    Thanks!

    I got it to work using a Coroutine. One more quick question, it happens faster than I would like, any ideas on how I could slow down the speed of the "arm chop" a little more? Here is my code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Rotate : MonoBehaviour
    6. {
    7.     public KeyCode pressAttack;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (Input.GetKeyDown(pressAttack))
    18.         {
    19.             StartCoroutine(ArmChop());
    20.         }
    21.  
    22.     }
    23.  
    24.     IEnumerator ArmChop()
    25.     {
    26.         { GetComponent<Transform>().eulerAngles = new Vector3(0, 0, 45);
    27.             yield return null;
    28.         }
    29.  
    30.         { GetComponent<Transform>().eulerAngles = new Vector3(0, 0, -45);
    31.             yield return null;
    32.         }
    33.  
    34.         {
    35.             GetComponent<Transform>().eulerAngles = new Vector3(0, 0, 0);
    36.             yield return null;
    37.         }
    38.     }
    39. }
    40.  
     
  4. TheFunnySide

    TheFunnySide

    Joined:
    Nov 17, 2018
    Posts:
    200
    Your Coroutine needs to compute the angle based on the time since it was started.
    And that is going to be a mess because you have a sequence of rotations you want to do.
    The condition for the while should also be a timer

    Code (CSharp):
    1.     IEnumerator ArmChop()
    2.     {
    3.  
    4. var timeStarted = Time.time;
    5.  
    6. while(itsNotOver)
    7. {
    8. var angle = computeAngleSomehow()
    9. GetComponent<Transform>().eulerAngles =angle ;
    10. yield return null;
    11. }
    12.    
    13.      
    14.     }