So far in my game, I have 3 attack forms: hand-to-hand combat, the summoning of a staff and the summoning of a scythe. What I’m working on currently is giving the player the ability to cycle through their attacks whenever they press the “S” key, but with the way I have it coded it doesn’t really work.
To make this simpler, I’ve put the chunk of code needed to be edited on it’s own script:
private Animator anim;
void Update()
{
//Summon staff
if (Input.GetKey(KeyCode.S))
{
anim.SetBool("summonStaff", true);
}
else
{
anim.SetBool("summonStaff", false);
}
//Scythe transition, which doesn't work of course because both animations basically play at the same time.
if (Input.GetKey(KeyCode.S))
{
anim.SetBool("switchToScythe", true);
}
else
{
}
}
}
Though I didn’t think it was necessary, in case it is I’ll put the rest of the code that has the player movements, the defense and attacks:
{
public float maxSpeed = 10f;
public float jumpHeight = 45f;
public float gravityScale = 11f;
private Animator anim;
float moveDirection = 0;
bool isGrounded = false;
Vector3 cameraPos;
Rigidbody2D rb;
Collider2D mainCollider;
// Check every collider except Player and Ignore Raycast
LayerMask layerMask = ~(1 << 2 | 1 << 8);
Transform t;
// Use this for initialization
void Start()
{
t = transform;
rb = GetComponent<Rigidbody2D>();
mainCollider = GetComponent<Collider2D>();
rb.freezeRotation = true;
rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
rb.gravityScale = gravityScale;
gameObject.layer = 8;
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
// Movement
if ((Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) && (isGrounded || rb.velocity.x > 0.01f))
{
moveDirection = Input.GetKey(KeyCode.A) ? -1 : 1;
}
else
{
if (isGrounded || rb.velocity.magnitude < 0.01f)
{
moveDirection = 0;
}
}
if (Input.GetKeyDown(KeyCode.W) && isGrounded)
{
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
}
//Defense
if (Input.GetKey(KeyCode.B))
{
anim.SetBool("isBlocking", true);
maxSpeed = 0f;
jumpHeight = 0f;
}
else
{
anim.SetBool("isBlocking", false);
maxSpeed = 10f;
jumpHeight = 45f;
}
//Attacks
//Simple Freeze
if (Input.GetKey(KeyCode.F))
{
anim.SetBool("isBlasting", true);
}
else
{
anim.SetBool("isBlasting", false);
}
}
void FixedUpdate()
{
Bounds colliderBounds = mainCollider.bounds;
Vector3 groundCheckPos = colliderBounds.min + new Vector3(colliderBounds.size.x * 0.5f, 0.1f, 0);
isGrounded = Physics2D.OverlapCircle(groundCheckPos, 0.23f, layerMask);
rb.velocity = new Vector2((moveDirection) * maxSpeed, rb.velocity.y);
}
}
If any other information is needed, I’ll be happy to provide.