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:
public class ManScript : MonoBehaviour
{
public Rigidbody2D myMan;
public float moveSpeed = 7;
public float activeMoveSpeed;
public float dashSpeed;
public float dashLength = .5f, dashCD = 1f;
private float dashCounter;
private float dashCooldown;
public float HP, MaxHP;
[SerializeField]
private HealthScript healthBar;
// Start is called before the first frame update
void Start()
{
activeMoveSpeed = moveSpeed;
healthBar.SetMaxHealth(MaxHP);
HP = MaxHP;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.D))
{
myMan.MovePosition(transform.position + transform.right * activeMoveSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.A))
{
myMan.MovePosition(transform.position - transform.right * activeMoveSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.W))
{
myMan.MovePosition(transform.position + transform.up * activeMoveSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.S))
{
myMan.MovePosition(transform.position - transform.up * activeMoveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
{
myMan.MovePosition(transform.position + (transform.up - transform.right).normalized * activeMoveSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
{
myMan.MovePosition(transform.position - (transform.up - transform.right).normalized * activeMoveSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A))
{
myMan.MovePosition(transform.position - (transform.up + transform.right).normalized * activeMoveSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
{
myMan.MovePosition(transform.position + (transform.up + transform.right).normalized * activeMoveSpeed * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (dashCooldown <= 0 && dashCounter <= 0)
{
activeMoveSpeed = dashSpeed;
dashCounter = dashLength;
}
}
if (dashCounter > 0)
{
dashCounter -= Time.deltaTime;
if (dashCounter <= 0)
{
activeMoveSpeed = moveSpeed;
dashCooldown = dashCD;
}
}
if (dashCooldown > 0)
{
dashCooldown -= Time.deltaTime;
}
if(Input.GetKeyDown(KeyCode.J))
{
SetHealth(-20f);
}
if(Input.GetKeyDown (KeyCode.K))
{
SetHealth(20f);
}
if(HP <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
public void SetHealth(float healthChange)
{
HP += healthChange;
HP = Mathf.Clamp(HP, 0, MaxHP);
healthBar.SetHealth(HP);
}
}
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
public class FistScript : MonoBehaviour
{
Rigidbody2D rb;
Vector2 initialPosition;
bool fistMovingBack;
public float fistSpeed = 50;
public ManScript manFloat;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
initialPosition = transform.position;
float value = manFloat.activeMoveSpeed;
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
transform.eulerAngles = Vector3.forward * 180;
}
if (Input.GetKeyDown(KeyCode.D))
{
transform.eulerAngles = Vector3.forward * 360;
}
if (Input.GetKeyDown(KeyCode.W))
{
transform.eulerAngles = Vector3.forward * 90;
}
if (Input.GetKeyDown(KeyCode.S))
{
transform.eulerAngles = Vector3.forward * 270;
}
if(Input.GetKeyDown(KeyCode.I))
{
Invoke("Punch", 1f);
}
}
void Punch()
{
}
}
Thanks already for answers!