Search Unity

New and need help with character controller 2d

Discussion in 'Game Design' started by Sekei014, Oct 13, 2019.

  1. Sekei014

    Sekei014

    Joined:
    Oct 13, 2019
    Posts:
    1
    Ok, so I'm very new to C# and Unity. My ColliderDistance2d keeps throwing the error 'cannot find type or namespace' and for Distance 'Collider2d does not contain definition for 'Distance''. I can't figure it out. It's for a tutorial and I'm not sure what I'm doing wrong. Here is the code.




    using UnityEngine;


    [RequireComponent(typeof(BoxCollider2D))]
    public class CharController2d : MonoBehaviour
    {

    [SerializeField, Tooltip("Max speed, in units per second, that the character moves.")]
    float speed = 9;

    [SerializeField, Tooltip("Acceleration while grounded.")]
    float walkAcceleration = 75;

    [SerializeField, Tooltip("Acceleration while in the air.")]
    float airAcceleration = 30;

    [SerializeField, Tooltip("Deceleration applied when character is grounded and not attempting to move.")]
    float groundDeceleration = 70;

    [SerializeField, Tooltip("Max height the character will jump regardless of gravity")]
    float jumpHeight = 4;

    private BoxCollider2D boxCollider;


    private Vector2 velocity;

    private bool grounded;

    private void Awake()
    {
    boxCollider = GetComponent<BoxCollider2D>();
    }

    private void Update()
    {
    if (grounded)
    {
    velocity.y = 0;

    if (Input.GetButtonDown("Jump"))
    {
    velocity.y = Mathf.Sqrt(2 * jumpHeight * Mathf.Abs(Physics2D.gravity.y));
    }
    }

    velocity.y += Physics2D.gravity.y * Time.deltaTime;

    float moveInput = Input.GetAxisRaw("Horizontal");

    float acceleration = grounded ? walkAcceleration : airAcceleration;
    float deceleration = grounded ? groundDeceleration : 0;

    if (moveInput != 0)
    {
    velocity.x = Mathf.MoveTowards(velocity.x, speed * moveInput, acceleration * Time.deltaTime);
    }
    else
    {
    velocity.x = Mathf.MoveTowards(velocity.x, 0, deceleration * Time.deltaTime);
    }

    transform.Translate(velocity * Time.deltaTime);

    Collider2D[] hits = Physics2D.OverlapBoxAll(transform.position, boxCollider.size, 0);

    grounded = false;

    foreach (Collider2D hit in hits)
    {
    if (hit == boxCollider)
    continue;

    ColliderDistance2D colliderDistance = hit.Distance(boxCollider);

    if (colliderDistance.isOverlapped)
    {
    transform.Translate(colliderDistance.pointA - colliderDistance.pointB);
    }

    if (Vector2.Angle(colliderDistance.normal, Vector2.up) < 90 && velocity.y < 0)
    {
    grounded = true;
    }
    }
    }
    }
     
  2. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    372
    You'll probably get help if you post in the scripting forum and use code tags.