Hey, i want to have this punching mechanic in my game, where the characters fist fly a short distance infront of him to attack. But my problem right now is that i dont know how to check what direction the player is facing right now, to make the fists go that way. Does anybody know how to do this? I also have diagonals in my game, which make it even harder for me, as a coding noob. Thanks already! I have managed to change the direction the fists are facing, since they are not actually on the main player sprite, but rather a child of the main player object.
how do you flip the player in your game?
oh yeah, sorry, should have clarified, since my player character is just a faceless block i opted to only make the fidts rotate using transform.eulerAngles and Vector3.forward multiplied by the corresponding degree for each button WASD, i cant remember the exact code right now since im not on my PC, i can post it tomorrow if it helps more to understand
so then you dont need to know what direction it is, its your player forward…
Oh, Alright! i didnt know that! Thank you very much, i will try tomorrow and report back if it worked!
Well, now im at a stop again, as i dont know how to make my fists return after getting shot out. I thought i could just use my Dash code but that will just shoot out without returning it. Anybody know how that would be possible? I toyed around with the idea of creating an initialPosition variable and setting it as the same as transform.position but i believe that will just return it to the place it shot from, not back to the player. So, any ideas?
Here is my code so far for the fists, mostly just for the rotation:
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()
{
}
}
And here my player script for reference ( i know its very messy, but im a noob and it works like this):
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);
}
}
Thanks already for helping!
Well you could send the fists back where they came from? or move it back as much as you moved it forward?
Well, the problem is, my player character is supposed to be constantly moving. So sending them back to where they came from wont work, since the player will not be there anymore. i want them to always return to the player after getting “shot” out. shot out is probably the wrong word, more like just punch forward a bit. I say shot out since the fists are like floating besides the character and are its own game objects, just a child from the player, so they move around with the player.
well not back to the world position but to the position relative to the toon yes…
Oh, how would that look like in code? Could you help me there?