Thank you for the reply, how would I go about moving Sonic with the Rigidbody2D? I ask because I thought I was already doing that, but must not have been.
Thanks.
Here’s my Script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveSonic : MonoBehaviour
{
private Rigidbody2D myRigidbody;
private Animator myAnimator;
[SerializeField]
private float movementSpeed;
private bool attack;
private bool facingRight;
[SerializeField]
private Transform[ ] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
private bool isGrounded;
private bool jump;
[SerializeField]
private bool airControl;
[SerializeField]
private float jumpForce;
public float jumpPower;
private float moveDirection;
private void Awake()
{
myRigidbody = GetComponent();
}
// Start is called before the first frame update
void Start()
{
facingRight = true;
myRigidbody = GetComponent();
myAnimator = GetComponent();
}
void Update()
{
HandleInput();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxis(“Horizontal”);
isGrounded = IsGrounded();
HandleMovement(horizontal);
Flip(horizontal);
HandleAttacks();
ResetValues();
}
private void HandleMovement(float horizontal)
{
if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag(“SpinDash”) && (isGrounded || airControl))
{
myRigidbody.velocity = new Vector2(horizontal * movementSpeed, myRigidbody.velocity.y);
}
if (isGrounded && jump)
{
isGrounded = false;
myRigidbody.AddForce(new Vector2(0, jumpForce));
myAnimator.SetTrigger(“jump”);
}
myAnimator.SetFloat(“speed”, Mathf.Abs(horizontal));
moveDirection = Input.GetAxis(“Horizontal”);
myRigidbody.AddForce(transform.right);
myRigidbody.velocity = new Vector2(moveDirection * movementSpeed, myRigidbody.velocity.y);
}
private void HandleAttacks()
{
if (attack && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag(“SonicJumpDash”))
{
myAnimator.SetTrigger(“spin attack”);
myRigidbody.velocity = Vector2.zero;
}
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpPower);
}
if (Input.GetKeyDown(KeyCode.RightShift))
{
attack = true;
}
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private void ResetValues()
{
attack = false;
jump = false;
}
private bool IsGrounded()
{
if (myRigidbody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[ ] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders*.gameObject != gameObject)*
{
myAnimator.ResetTrigger(“jump”);
return true;
}
}
}
}
return false;
}
}