Hello! I have a problem with rigidbody.AddForce. I have a character movescript and the player looks at the cursor, so on pressing the “Jump” key it should add force in the direction where the player looks. And it only kinda works. link to gif : Imgur: The magic of the Internet
code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float movementSpeed = 5f;
public float rollforce = 15f;
public Rigidbody2D rb;
public Camera cam;
Vector2 movement;
Vector2 mousePos;
private bool isrolling;
void Update()
{
if (Input.GetButtonDown("Jump"))
{
isrolling = true;
}
if (Input.GetButtonUp("Jump"))
{
isrolling = false;
}
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
private void FixedUpdate()
{
if (isrolling == true)
{
Debug.Log("pressed");
//rb.AddForce(transform.forward * rollforce, ForceMode2D.Impulse);
rb.AddRelativeForce(transform.up * rollforce, ForceMode2D.Force);
}
if (isrolling == false)
{
rb.MovePosition(rb.position + movement * movementSpeed * Time.fixedDeltaTime);
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
}
}