So I’m new to Unity and new to C#, so please forgive my naivety.
I need a little help with two things here, and I believe they are connected.
VIDEO HERE:
https://imgur.com/O4zXHhP
-
The first is I have a child game object that is animated based on the parent’s movement. As you can see from the video above, it is sliding from hand to hand when the animations change. I’m not sure what to do about this. When the player stops, unless travelling down or down right, the sword floats back to the center of the player. Just in case you’re wondering, I did not forget about the idle states either.
-
The player does this odd thing where the animation flickers left when the movement key is released. It’s most notable when moving up-right, because it flashed the down left face.
Here is my player movement script (removed a little that wasn’t relevant, just to keep focus on the movement and animation):
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;
//Animations
public Animator topAnimator;
public Animator bottomAnimator;
public GameObject crossHair;
//Movement and Aiming
Vector2 movement;
Vector2 aim;
private float lastDirX;
private float lastDirY;
bool isAiming;
bool endAiming;
public float speed;
public float arrowSpeed;
//LERP Dodge Roll
public bool isDodging;
private float fracJourney;
public Vector2 startMarker;
public Vector2 endMarker;
public float dodgeSpeed;
public float dodgeDistance;
public float animationSpeed;
//Combat System
public static int health;
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;
health = 3;
}
// Update is called once per frame
void Update()
{
ProcessInputs();
AimAndShoot();
Animate();
}
private void FixedUpdate()
{
Move();
Dodge();
}
//Moving
private void Move()
{
//transform.position = transform.position + movement * speed * Time.deltaTime;
rb.MovePosition(rb.position + (movement * speed * Time.fixedDeltaTime));
rb.velocity = new Vector2(movement.x, movement.y);
}
}
//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("Fire1");
endAiming = Input.GetButtonUp("Fire1");
//}
if (movement.magnitude > 1.0f)
{
movement.Normalize();
}
//Set Last Direction Parameter (for Idle Facing)
if (Input.GetKey(KeyCode.W))
{
lastDirX = 0;
lastDirY = 1;
}
else if (Input.GetKey(KeyCode.A))
{
lastDirX = -1;
lastDirY = 0;
}
else if (Input.GetKey(KeyCode.S))
{
lastDirX = 0;
lastDirY = -1;
}
else if (Input.GetKey(KeyCode.D))
{
lastDirX = 1;
lastDirY = 0;
};
}
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", movement.x);
topAnimator.SetFloat("MoveVertical", movement.y);
topAnimator.SetFloat("MoveMagnitude", movement.magnitude);
//Aiming
topAnimator.SetFloat("AimHorizontal", aim.x);
topAnimator.SetFloat("AimVertical", aim.y);
topAnimator.SetFloat("AimMagnitude", aim.magnitude);
topAnimator.SetBool("Aim", isAiming);
//Idle Direction
topAnimator.SetFloat("LastMoveX", lastDirX);
topAnimator.SetFloat("LastMoveY", lastDirY);
}