Search Unity

Question Does any one know how to make sword swing animation like archvale game ?

Discussion in '2D' started by Ddoll, May 28, 2020.

  1. Ddoll

    Ddoll

    Joined:
    May 27, 2020
    Posts:
    5
    Hello! I want to make something similar to Archvale's weapon rotation. i have made the weapon rotating the player only i need is when i left click the sword will flip.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Sword : MonoBehaviour
    6. {
    7.     // Point you want to have sword rotate around
    8.     public Transform shoulder;
    9.     // how far you want the sword to be from point
    10.     public float armLength = 1f;
    11.     bool facingright = false;
    12.     // Start is called before the first frame update
    13.     public KeyCode attack1;
    14.     void Start()
    15.     {
    16.         // shoulder = transform.parent.transform;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         // Get the direction between the shoulder and mouse (aka the target position)
    23.         Vector3 shoulderToMouseDir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - shoulder.position;
    24.         shoulderToMouseDir.z = 0; // zero z axis since we are using 2d
    25.                                   // we normalize the new direction so you can make it the arm's length
    26.                                   // then we add it to the shoulder's position
    27.  
    28.         Vector3 mousePosition = Input.mousePosition;
    29.         // mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
    30.         // Vector2 direction = new Vector2(mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
    31.         // transform.right = direction;
    32.         //Flip
    33.         // Vector3 SwordScale = transform.localScale;
    34.         // if (mousePosition.x < transform.position.x)
    35.         // {
    36.         //     SwordScale.y = -1;
    37.         //     SwordScale.x = -1;
    38.         // }
    39.         // else if (mousePosition.x > transform.position.x)
    40.         // {
    41.         //     SwordScale.y = 1;
    42.         //     SwordScale.x = 1;
    43.         // }
    44.        
    45.         // transform.localScale = SwordScale;
    46.         transform.position = shoulder.position - (armLength * shoulderToMouseDir.normalized);
    47.         if (Input.GetKeyDown(attack1) && !facingright){
    48.             // SwordScale.y = -1;
    49.             facingright = true;
    50.         }
    51.         else if(Input.GetKeyDown(attack1) && facingright){
    52.             // SwordScale.y = 1;
    53.             facingright = false;
    54.         }
    55.     }
    56. }
    57.  
     
    emovagina likes this.
  2. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    I mean if all you want to flip the sword sprite changing their scale to the negative should be all you need to do, looking at the code is it not what the commented out code does?
     
  3. Ddoll

    Ddoll

    Joined:
    May 27, 2020
    Posts:
    5
    it's just flip it doesn't feel right. it doesn't change sa position like in the video. but if i change the position it won't move
     
  4. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    If you could change at what point it flips, might help? Then you can change its pivot point so it rotate at where you want.

    Else there is using animation to move it around and parent it to the character so it always near the character, should be able to move it however you want?