Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Throw out a sprite that returns to the player

Discussion in 'Getting Started' started by Kr00mmi, Apr 22, 2024.

  1. Kr00mmi

    Kr00mmi

    Joined:
    Apr 20, 2024
    Posts:
    10
    Hey, im kinda at my wits end here. In the attached file is my player character. Right now i am trying to code a mechanic where i can shoot out the fists that are attached to the player. But i cant figure out how to make them return to the player, who is constantly moving. i toyed around but only got the to return to the position they original position the were thrown out from. The throwing out itself is not the problem, since i can just copy and modify my dash mechanic script. My main problem is returning them to the player. I should add, the fists itself are its own gameobject, as a child of the player object

    Ill add both player and Fist script for reference.

    Here is the player script:
    Code (CSharp):
    1. public class ManScript : MonoBehaviour
    2. {
    3.     public Rigidbody2D myMan;
    4.     public float moveSpeed = 7;
    5.  
    6.     public float activeMoveSpeed;
    7.     public float dashSpeed;
    8.  
    9.     public float dashLength = .5f, dashCD = 1f;
    10.  
    11.     private float dashCounter;
    12.     private float dashCooldown;
    13.  
    14.     public float HP, MaxHP;
    15.  
    16.  
    17.     [SerializeField]
    18.     private HealthScript healthBar;
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         activeMoveSpeed = moveSpeed;
    23.         healthBar.SetMaxHealth(MaxHP);
    24.         HP = MaxHP;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.      
    31.      
    32.         if (Input.GetKey(KeyCode.D))
    33.         {
    34.             myMan.MovePosition(transform.position + transform.right * activeMoveSpeed * Time.deltaTime);
    35.  
    36.         }
    37.         else if (Input.GetKey(KeyCode.A))
    38.         {
    39.             myMan.MovePosition(transform.position - transform.right * activeMoveSpeed * Time.deltaTime);
    40.  
    41.         }
    42.  
    43.  
    44.         else if (Input.GetKey(KeyCode.W))
    45.         {
    46.             myMan.MovePosition(transform.position + transform.up * activeMoveSpeed * Time.deltaTime);
    47.          
    48.  
    49.         }
    50.         else if (Input.GetKey(KeyCode.S))
    51.         {
    52.             myMan.MovePosition(transform.position - transform.up * activeMoveSpeed * Time.deltaTime);
    53.          
    54.  
    55.         }
    56.  
    57.  
    58.         if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
    59.         {
    60.             myMan.MovePosition(transform.position + (transform.up - transform.right).normalized * activeMoveSpeed * Time.deltaTime);
    61.         }
    62.  
    63.         else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
    64.         {
    65.             myMan.MovePosition(transform.position - (transform.up - transform.right).normalized * activeMoveSpeed * Time.deltaTime);
    66.         }
    67.  
    68.         else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A))
    69.         {
    70.             myMan.MovePosition(transform.position - (transform.up + transform.right).normalized * activeMoveSpeed * Time.deltaTime);
    71.         }
    72.  
    73.         else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
    74.         {
    75.             myMan.MovePosition(transform.position + (transform.up + transform.right).normalized * activeMoveSpeed * Time.deltaTime);
    76.         }
    77.  
    78.         if (Input.GetKeyDown(KeyCode.Space))
    79.         {
    80.             if (dashCooldown <= 0 && dashCounter <= 0)
    81.             {
    82.                 activeMoveSpeed = dashSpeed;
    83.                 dashCounter = dashLength;
    84.             }
    85.         }
    86.         if (dashCounter > 0)
    87.         {
    88.             dashCounter -= Time.deltaTime;
    89.  
    90.             if (dashCounter <= 0)
    91.             {
    92.                 activeMoveSpeed = moveSpeed;
    93.                 dashCooldown = dashCD;
    94.  
    95.             }
    96.         }
    97.         if (dashCooldown > 0)
    98.         {
    99.             dashCooldown -= Time.deltaTime;
    100.         }
    101.         if(Input.GetKeyDown(KeyCode.J))
    102.         {
    103.             SetHealth(-20f);
    104.         }
    105.         if(Input.GetKeyDown (KeyCode.K))
    106.         {
    107.             SetHealth(20f);
    108.         }
    109.  
    110.         if(HP <= 0)
    111.         {
    112.             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    113.         }
    114.  
    115.      
    116.  
    117.  
    118.  
    119.     }
    120.    public void SetHealth(float healthChange)
    121.     {
    122.         HP += healthChange;
    123.         HP = Mathf.Clamp(HP, 0, MaxHP);
    124.  
    125.         healthBar.SetHealth(HP);
    126.     }
    127.      
    128. }
    129.  
    And here the Fist script, which is only to change the rotation of them to wherever im moving, for now, but like i said i want to add a mechanic where they shoot out a bit infrot of the player and return instantly after reaching the furthest point

    Code (CSharp):
    1. public class FistScript : MonoBehaviour
    2. {
    3.     Rigidbody2D rb;
    4.     Vector2 initialPosition;
    5.     bool fistMovingBack;
    6.     public float fistSpeed = 50;
    7.     public ManScript manFloat;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody2D>();
    13.         initialPosition = transform.position;
    14.         float value = manFloat.activeMoveSpeed;
    15.      
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if(Input.GetKeyDown(KeyCode.A))
    23.         {
    24.             transform.eulerAngles = Vector3.forward * 180;
    25.         }
    26.  
    27.         if (Input.GetKeyDown(KeyCode.D))
    28.         {
    29.             transform.eulerAngles = Vector3.forward * 360;
    30.         }
    31.  
    32.         if (Input.GetKeyDown(KeyCode.W))
    33.         {
    34.             transform.eulerAngles = Vector3.forward * 90;
    35.         }
    36.         if (Input.GetKeyDown(KeyCode.S))
    37.         {
    38.             transform.eulerAngles = Vector3.forward * 270;
    39.         }
    40.  
    41.         if(Input.GetKeyDown(KeyCode.I))
    42.         {
    43.             Invoke("Punch", 1f);
    44.         }
    45.     }
    46.     void Punch()
    47.         {
    48.          
    49.         }
    50. }
    51.  


    Thanks already for answers!
     

    Attached Files:

  2. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    136
    Consider breaking your fists into two completely separate types: "fake" fists that are attached to the player as he moves around, and "real" fists that are launched out and can damage/interact with enemies/objects before returning to the "fake" fist location.

    You already have the "fake" fists in place. These should merely be a graphical representation of the player's fists. They should not have a collider or Rigidbody.

    When it's time to launch the "real" fists, all you have to do is 1.) hide the "fake" fists by disabling their Sprite Renderers, and 2.) instantiate the "real" fists at the fake ones' location before launching them outwards.

    Each "real" fist's script should contain a reference to the "fake" fist from which it originated. This reference should be initialized when each "real" fist is instantiated. When it's time to return to the player, just move it towards that referenced object.

    Once both "real" fists have finished returning to the player, 1.) destroy the "real" fists, and 2.) un-hide the "fake" fists by enabling their Sprite Renderers.
     
  3. Kr00mmi

    Kr00mmi

    Joined:
    Apr 20, 2024
    Posts:
    10
    Thank you very much! That sounds very logical! I will try to code it myself first, but ill probably will struggle somewhere so ill update later!
     
  4. Kr00mmi

    Kr00mmi

    Joined:
    Apr 20, 2024
    Posts:
    10
    Yeah, im already at a loss. Im guessing i have to create 2 new game objects for the real fists as child objects from the player, but i dont really know how to instantiate them at the fake ones location and hiding the old ones, and i also dont know how to return them to the fake fists, Could you maybe help me with some of the code?