using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class foxNinja_Controller : MonoBehaviour
{
Rigidbody2D rb;
Animator animator;
Transform groundCheckCollider;
public LayerMask groundLayer = new LayerMask();
const float groundCheckRadius = 0.2f;
[SerializeField] float speed = 400;
float horizontalValue;
float runSpeedModifier = 2f;
bool isGrounded = false;
bool isRunning = false;
bool facingRight = true;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Update()
{
horizontalValue = Input.GetAxisRaw("Horizontal");
//To check if player is running or not
if (Input.GetKeyDown(KeyCode.LeftShift))
isRunning = true;
if (Input.GetKeyUp(KeyCode.LeftShift))
isRunning = false;
}
void FixedUpdate()
{
GroundCheck();
Move(horizontalValue);
}
void GroundCheck()
{
isGrounded = false;
//Check if GroundCheckObject is colliding with other colliders in Ground layer
//If yes isGrounded is true and vice versa
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, groundCheckRadius, groundLayer);
if(colliders.Length>0)
{
isGrounded = true;
}
}
void Move(float dir)
{
float xVal = dir * speed * 100 * Time.fixedDeltaTime;
//Running Multiplier
if (isRunning)
xVal *= runSpeedModifier;
Vector2 targetVelocity = new Vector2(xVal, rb.velocity.y);
rb.velocity = targetVelocity;
//look flip(left/right)
if (facingRight && dir < 0)
{
transform.localScale = new Vector3(-6, 6, 1);
facingRight = false;
}
if (!facingRight && dir > 0)
{
transform.localScale = new Vector3(6, 6, 1);
facingRight = true;
}
// 0 idle, 2.5 walking, 5 running
//Set the float xVelocity acc. to the x value of the RigidBody2D Velocity
animator.SetFloat("xVelocity", Mathf.Abs(rb.velocity.x));
}
}
I’m new so I don’t know much, apologizing in advance.
I’m making a 2D character controller, and I can’t see the ground check collider in the inspector, also a warning is popping as mentioned in the title. Please Help :(
The groundCheckCollider variable is not set to public or serialized so that is why you can’t see it in the inspector. And, since you never set it in the code either, it won’t have a value and is left in an unassigned state and thus is unsafe to use as god knows what its value is.
I serialized it earlier but it didn’t do anything. I did it again after your reply but still no change in the inspector.
;(
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class foxNinja_Controller : MonoBehaviour
{
Rigidbody2D rb;
Animator animator;
[SerializeField] public Transform groundCheckCollider;
public LayerMask groundLayer = new LayerMask();
[SerializeField] const float groundCheckRadius = 0.2f;
[SerializeField] float speed = 400;
float horizontalValue;
float runSpeedModifier = 2f;
[SerializeField] bool isGrounded = false;
bool isRunning = false;
bool facingRight = true;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Update()
{
horizontalValue = Input.GetAxisRaw("Horizontal");
//To check if player is running or not
if (Input.GetKeyDown(KeyCode.LeftShift))
isRunning = true;
if (Input.GetKeyUp(KeyCode.LeftShift))
isRunning = false;
}
void FixedUpdate()
{
GroundCheck();
Move(horizontalValue);
}
void GroundCheck()
{
isGrounded = false;
//Check if GroundCheckObject is colliding with other colliders in Ground layer
//If yes isGrounded is true and vice versa
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, groundCheckRadius, groundLayer);
if(colliders.Length>0)
{
isGrounded = true;
}
}
void Move(float dir)
{
float xVal = dir * speed * 100 * Time.fixedDeltaTime;
//Running Multiplier
if (isRunning)
xVal *= runSpeedModifier;
Vector2 targetVelocity = new Vector2(xVal, rb.velocity.y);
rb.velocity = targetVelocity;
//look flip(left/right)
if (facingRight && dir < 0)
{
transform.localScale = new Vector3(-6, 6, 1);
facingRight = false;
}
if (!facingRight && dir > 0)
{
transform.localScale = new Vector3(6, 6, 1);
facingRight = true;
}
// 0 idle, 2.5 walking, 5 running
//Set the float xVelocity acc. to the x value of the RigidBody2D Velocity
animator.SetFloat("xVelocity", Mathf.Abs(rb.velocity.x));
}
}
Update:
It was an issue with unity not with the code.
Made a new script, working fine now.
If you can still replicate the original, you should send it to Unity team as a bug report.
Done. Thanks for your help.