- Home /
Need help with physics based 2D character [SOLVED]
I have a character (chouette) that can jump twice in the direction of the mouse pointer when mouse0 is clicked. When he hits the ground the movement script is temporarely disabled to let the sprite roll around with physics, and the movement is re-enabled when mouse0 is clicked. Now here's my problem, if i dont click and release mouse0 very quiclky , the second jump is instantly used and my character cant double jump. So i tried fixing this with Input.GetMouseButton(0) -> Input.GetMouseButton*Down*(0) so the function only plays each time I press AND release mouse0, but unfortunately my character cannot jump anymore. here's Mouse_Jump.cs
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mouse_Jump : MonoBehaviour
{
private Vector3 mousePosition;
private Rigidbody2D chouette;
private Vector2 direction;
private SpriteRenderer mySpriteRenderer;
public float angle;
public float force = 20f;
public float jump_Amount = 1;
public float jumps_Left = 1;
public bool isGrounded;
public LayerMask groundLayers;
public Ragdoll_activation ragdoll;
void Start()
{
mySpriteRenderer = GetComponent<SpriteRenderer>();
chouette = GetComponent<Rigidbody2D>();
}
void Update()
{
// orientation / direction chouette
transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
if (Input.GetMouseButton(0) && jumps_Left > 0)
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = (mousePosition - transform.position).normalized;
chouette.velocity = new Vector2(direction.x * force, direction.y * force);
jumps_Left--;
}
else if (Input.GetMouseButton(0) && jumps_Left == 0 && isGrounded == true)
{
chouette.velocity = new Vector2(direction.x * force, direction.y * force);
}
// flip sprite chouette
angle = (Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg);
if ((angle > 90 && angle < 180))
{
mySpriteRenderer.flipX = true;
}
else if (angle < 90 && angle > 0 )
{
mySpriteRenderer.flipX = false;
}
// Groundcheck
isGrounded = Physics2D.OverlapArea(new Vector2(transform.position.x - 0.5f, transform.position.y - 0.5f),
new Vector2(transform.position.x + 0.5f, transform.position.y - 0.51f), groundLayers);
//ragdoll
if(isGrounded == true)
{
jumps_Left = jump_Amount;
ragdoll.activer();
}
}
}
and here is Ragdoll_activation.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ragdoll_activation : MonoBehaviour
{
public void activer()
{
gameObject.GetComponent<Mouse_Jump>().enabled = false;
}
void Update()
{
if (Input.GetMouseButton(0))
{
gameObject.GetComponent<Mouse_Jump>().enabled = true;
Debug.Log("deac rag");
}
}
}
If my code really is that stupid, are there any other ways to make my character a physics based object if no imput is applied/ when it hits the gound?
Update !! I solved it by getting completely rid of the "ragdoll_activation" script and by moving "transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);" inside the "if" statement. Finally i changed "chouette.velocity" to "chouette.AddForce" . (chouette is a rigidBody2D)
Answer by ahsen35813 · Aug 26, 2020 at 04:46 PM
I'm too lazy too read through all the code in detail, but I have one idea. Perhaps you can do something like this:
void Update()
{
if (onGround /*You'd have to implement this variable yourself*/ || !Input.GetMouseButton(0))
{
rb.enabled = true;
StartCoroutine(Wait());
}
else
{
rb.enabled = false;
}
}
IEnumerator Wait()
{
yield return new WaitForSeconds(0.5f);
rb.enabled = false;
}
Basically, this code will enabled the rigidbody for a certain number of seconds when you hit the ground or when you are not clicking and disables the rigidbody when the player is not on the ground. You can put more variables in the if function, and so on. I hope this helps, and I'm sorry if I misunderstood the question!
Thank your for the response but unfortunately "rb does not contain a defenition for 'enabled', i have the latest version of Unity so I dont know why .enabled returns and error so i can't try out your code to see if it works
Answer by C_est_Chouette · Aug 26, 2020 at 09:43 PM
Update !! I solved it by getting completely rid of the "ragdoll_activation" script and by moving "transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);" inside the "if" statement. Finally i changed "chouette.velocity" to "chouette.AddForce" . (chouette is a rigidBody2D)
Your answer
Follow this Question
Related Questions
game hags with several Polygon Colliders on the scene 0 Answers
OnTriggerEnter2D first collision causes lag/freeze 1 Answer
How can I make edge collider to behave like normal(polygon) colyder? 0 Answers
How to pick up an object and make it follow the cursor, but realistically? 0 Answers
How to calculate angular velocity after a collision? 0 Answers