#help

1 messages · Page 1 of 1 (latest)

brisk quiver
#

I can't send my code what the heck

marsh terrace
brisk quiver
#
public class ForceRedMovement : MonoBehaviour
{
    public float currentSpeed;
    public float speed = 5f;
    public float maxSpeed = 15f;


    private float h;
    private float v;
    private Rigidbody2D rigidbody2d;
    private CircleCollider2D boxCollider2d;
    private SpriteRenderer sr;
    [SerializeField] private LayerMask platformsLayerMask;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2d = transform.GetComponent<Rigidbody2D>();
        boxCollider2d = transform.GetComponent<CircleCollider2D>();
        sr = transform.GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        h = Input.GetAxis("H1");
        v = Input.GetAxis("V1");
    }

    void FixedUpdate()
    {
        ApplyForceToReachVelocity(rigidbody2d, new Vector2(h * speed, 0f), speed);
        ApplyForceToReachVelocityV(rigidbody2d, new Vector2(v * speed, 0f), speed);
        currentSpeed = rigidbody2d.velocity.magnitude;


    }





    public static void ApplyForceToReachVelocity(Rigidbody2D rigidbody, Vector2 velocity, float force = 1, ForceMode2D mode = ForceMode2D.Force)
    {
        if (force == 0 || velocity.magnitude == 0)
            return;

        velocity = velocity + velocity.normalized * 0.2f * rigidbody.drag;

        //force = 1 => need 1 s to reach velocity (if mass is 1) => force can be max 1 / Time.fixedDeltaTime
        force = Mathf.Clamp(force, -rigidbody.mass / Time.fixedDeltaTime, rigidbody.mass / Time.fixedDeltaTime);

        if (rigidbody.velocity.magnitude == 0)
        {
            rigidbody.AddForce(velocity * force, mode);
        }
        else
        {
            var velocityProjectedToTarget = (velocity.normalized * Vector3.Dot(velocity, rigidbody.velocity) / velocity.magnitude);
            rigidbody.AddForce((velocity - velocityProjectedToTarget) * force, mode);
        }
    }


    public static void ApplyForceToReachVelocityV(Rigidbody2D rigidbody, Vector2 velocity, float force = 1, ForceMode2D mode = ForceMode2D.Force)
    {
        if (force == 0 || velocity.magnitude == 0)
            return;

        velocity = velocity + velocity.normalized * 0.2f * rigidbody.drag;

        //force = 1 => need 1 s to reach velocity (if mass is 1) => force can be max 1 / Time.fixedDeltaTime
        force = Mathf.Clamp(force, -rigidbody.mass / Time.fixedDeltaTime, rigidbody.mass / Time.fixedDeltaTime);

        if (rigidbody.velocity.magnitude == 0)
        {
            rigidbody.AddForce(velocity * force, mode);
        }
        else
        {
            var velocityProjectedToTarget = (velocity.normalized * Vector3.Dot(velocity, rigidbody.velocity) / velocity.magnitude);
            rigidbody.AddForce((velocity - velocityProjectedToTarget) * force, mode);
        }
    }







}


#

my code only moves the character horizontally and not vertically