I’m having a new issue today that I can’t seem to explain. Issue is that my 2D player character is moving ever so slightly every time I fire an arrow. Here’s a video for reference:
This is a new bug, but I’ve done so much I’m not sure where to look back to. I haven’t adjusted the player code or animation surrounding the bow and arrow, so I’m feeling pretty confused by it.
Here’s the current player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Rewired;
public class BitchFaceController : MonoBehaviour
{
public int playerId = 0;
private Player player;
public bool useController;
public Rigidbody2D rb;
public GameObject arrowPrefab;
//Animators
public Animator topAnimator;
public Animator bottomAnimator;
public GameObject crossHair;
//Movement
Vector2 movement;
public float speed;
private float lastDirX;
private float lastDirY;
private float currDirX;
private float currDirY;
//Bow
Vector2 aim;
bool isAiming;
bool endAiming;
public float arrowSpeed;
//Sword
Vector2 crossHairDif;
Vector2 crossHairDir;
bool isSlicing;
bool endSlicing;
private float crossHairDirX;
private float crossHairDirY;
private float crossHairDirXAnim;
private float crossHairDirYAnim;
//LERP Dodge Roll
public bool isDodging;
private float fracJourney;
public Vector2 startMarker;
public Vector2 endMarker;
public float dodgeSpeed;
public float dodgeDistance;
public float animationSpeed;
private void Awake()
{
Debug.Log("PlayerId is " + playerId.ToString());
player = ReInput.players.GetPlayer(playerId);
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Start()
{
rb = this.GetComponent<Rigidbody2D>();
arrowSpeed = 3;
}
// Update is called once per frame
void Update()
{
ProcessInputs();
AimAndShoot();
Animate();
}
private void FixedUpdate()
{
Move();
Dodge();
}
//Moving
private void Move()
{
rb.MovePosition(rb.position + (movement * speed * Time.fixedDeltaTime));
rb.velocity = new Vector2(movement.x, movement.y);
}
//Set direction parameters for sword slicing animation blend tree coordinates
void SwordAnimation()
{
crossHairDif = transform.position - crossHair.transform.position;
if (crossHairDif.magnitude < 1)
{
crossHairDir = crossHairDif.normalized;
}
crossHairDirX = -crossHairDir.x;
crossHairDirY = -crossHairDir.y;
}
//Dodging
private void Dodge()
{
if (!isDodging && Input.GetKey(KeyCode.Space) && (rb.velocity.x != 0 || rb.velocity.y != 0))
{
isDodging = true;
fracJourney = 0;
//startMarker = transform.position;
startMarker = rb.position;
endMarker = rb.position + movement * dodgeDistance;
//endMarker = rb.MovePosition(rb.position + (movement * dodgeDistance));
topAnimator.SetBool("Dodge", true);
}
if (isDodging)
{
fracJourney = fracJourney + Time.deltaTime * dodgeSpeed;
if (fracJourney > 1)
{
fracJourney = 1;
isDodging = false;
topAnimator.SetBool("Dodge", false);
//this.Archer_Bottom.enabled = true;
}
rb.MovePosition(Vector2.Lerp(startMarker, endMarker, fracJourney));
}
}
//Processing Inputs, Aiming, and Walk Animation
private void ProcessInputs()
{
movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
Vector2 mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
aim = aim + mouseMovement;
if (aim.magnitude > 1.0f)
{
aim.Normalize();
}
isAiming = Input.GetButton("Fire2");
endAiming = Input.GetButtonUp("Fire2");
isSlicing = Input.GetButtonDown("Fire1");
if (movement.magnitude > 1.0f)
{
movement.Normalize();
}
//Set direction parameters for moving and idling
if (Input.GetKey(KeyCode.W))
{
currDirX = 0;
currDirY = 1;
lastDirX = 0;
lastDirY = 1;
}
else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
{
currDirX = -1;
currDirY = 1;
}
else if (Input.GetKey(KeyCode.A))
{
currDirX = -1;
currDirY = 0;
lastDirX = -1;
lastDirY = 0;
}
else if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.S))
{
currDirX = -1;
currDirY = -1;
}
else if (Input.GetKey(KeyCode.S))
{
currDirX = 0;
currDirY = -1;
lastDirX = 0;
lastDirY = -1;
}
else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
{
currDirX = 1;
currDirY = -1;
}
else if (Input.GetKey(KeyCode.D))
{
currDirX = 1;
currDirY = 0;
lastDirX = 1;
lastDirY = 0;
}
else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.W))
{
currDirX = 1;
currDirY = 1;
};
SwordAnimation();
//Set direction parameters for sword animation
if (crossHairDirX < 0)
{
crossHairDirXAnim = -1;
}
if (crossHairDirX > 0)
{
crossHairDirXAnim = 1;
}
if (crossHairDirY <= 0.5f && crossHairDirY >= -0.5f)
{
crossHairDirYAnim = 0;
}
if (crossHairDirY < -0.5f)
{
crossHairDirYAnim = -1;
}
if (crossHairDirY > 0.5f)
{
crossHairDirYAnim = 1;
}
}
private void Animate()
{
//Moving Legs
bottomAnimator.SetFloat("MoveHorizontal", movement.x);
bottomAnimator.SetFloat("MoveVertical", movement.y);
bottomAnimator.SetFloat("MoveMagnitude", movement.magnitude);
//Moving Body
topAnimator.SetFloat("MoveHorizontal", currDirX);
topAnimator.SetFloat("MoveVertical", currDirY);
topAnimator.SetFloat("MoveMagnitude", movement.magnitude);
//Shooting
topAnimator.SetFloat("AimHorizontal", aim.x);
topAnimator.SetFloat("AimVertical", aim.y);
topAnimator.SetFloat("AimMagnitude", aim.magnitude);
topAnimator.SetBool("Aim", isAiming);
//Slicing
topAnimator.SetFloat("CrossHairDirX", crossHairDirXAnim);
topAnimator.SetFloat("CrossHairDirY", crossHairDirYAnim);
topAnimator.SetBool("SwordSlice", isSlicing);
//Idle Direction
topAnimator.SetFloat("LastMoveX", lastDirX);
topAnimator.SetFloat("LastMoveY", lastDirY);
}
private void AimAndShoot()
{
Vector2 shootingDirection = new Vector2(aim.x, aim.y);
if (aim.magnitude > 0.0f)
{
crossHair.transform.localPosition = aim * 0.4f;
crossHair.SetActive(true);
shootingDirection.Normalize();
if (endAiming)
{
GameObject arrow = Instantiate(arrowPrefab, transform.position, Quaternion.identity);
Arrow1 arrow1Script = arrow.GetComponent<Arrow1>();
arrow1Script.velocity = shootingDirection * arrowSpeed;
arrow1Script.player = gameObject;
arrow.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
Destroy(arrow, 1.0f);
}
}
else
{
crossHair.SetActive(false);
}
}
}