So im trying to use a PlayerController script up combined with a ColliderControl script in a 2D Platformer so I can just duplicate my player and either use them as other players or NPC’s / Enemies.
I made a Crash Test Dummy and use the same script but it’s effecting the new character in a different way.
I read that using the “this” function may help in specifying the code to only effect that specific sprite but to be honest im a newbie and could use some help with it, i’m guessing both scripts may need to use the “this” function? knowing me probably not though lol:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
public KeyCode left;
public KeyCode right;
public KeyCode jump;
public KeyCode throwNut;
private Rigidbody2D theRB;
public Transform groundCheckPoint;
public float groundCheckRadius;
public LayerMask whatIsGround;
public Transform CeilingCheck;
public bool isGrounded;
private bool isCrouched;
private float crouch;
public bool crouching;
private Animator anim;
public GameObject throwingNut;
public Transform ThrowPoint;
// Start is called before the first frame update
void Start()
{
theRB = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatIsGround);
isCrouched = Physics2D.OverlapCircle(CeilingCheck.position, groundCheckRadius, whatIsGround);
crouch = Input.GetAxisRaw("CrouchInput");
CrouchFunction();
GetComponent<Animator>().SetBool("Crouch", crouching);
if(Input.GetKey(throwNut))
{
GameObject nutClone = (GameObject)Instantiate(throwingNut, ThrowPoint.position, ThrowPoint.rotation);
nutClone.transform.localScale = transform.localScale;
anim.SetTrigger("Throw");
}
if (Input.GetKey(left))
{
theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
}
else if (Input.GetKey(right))
{
theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
}
else
{
theRB.velocity = new Vector2(0, theRB.velocity.y);
}
if (Input.GetKeyDown(jump) && isGrounded)
{
theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
}
if (theRB.velocity.x < 0)
{
transform.localScale = new Vector3(-1, 1, 1);
}
else if(theRB.velocity.x > 0)
{
transform.localScale = new Vector3(1, 1, 1);
}
anim.SetFloat("Speed", Mathf.Abs(theRB.velocity.x));
anim.SetBool("Grounded", isGrounded);
}
void CrouchFunction()
{
if((crouch!=0 || isCrouched==true) && isGrounded == true)
{
crouching = true;
moveSpeed = 1.5f;
}
else
{
crouching = false;
moveSpeed = 6f;
}
}
}
My Collider Control script is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColliderControl : MonoBehaviour
{
public BoxCollider2D stand;
public BoxCollider2D crouch;
public CircleCollider2D circle;
PlayerController playerC;
// Start is called before the first frame update
void Start()
{
playerC = GetComponent<PlayerController>();
stand.enabled = true;
crouch.enabled = false;
circle.enabled = true;
}
// Update is called once per frame
void Update()
{
if(playerC.isGrounded == false)
{
stand.enabled = true;
crouch.enabled = false;
circle.enabled = true;
}
else
{
if(playerC.crouching == true)
{
stand.enabled = false;
crouch.enabled = true;
circle.enabled = true;
}
else
{
stand.enabled = true;
crouch.enabled = false;
circle.enabled = true;
}
}
}
}
Also was wondering how I set up this exact crouching method using a Public KeyCode, as my code currently uses GetAxisRaw for it and I want to be able to assign it in the inspector like the rest of the controls, the crouch code uses the ColliderControl script to let me set the ceiling and floor detection using 2 circle colliders, and will switch between a “standing” box collider and a “crouching” box collider when crouch is pressed. but on player 2 the ground and ceiling checks transform location once i hit play and he just appears in the air, with the checks about an inch higher above and an inch below than i set them.
Player 1 is unaffected and works like a charm so far running off the same scripts.
I’m brand new to Unity and need help lol!
Thanks all!