Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by C_est_Chouette · Aug 26, 2020 at 04:14 PM · physics 2d

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image C_est_Chouette · Aug 26, 2020 at 09:44 PM 0
Share

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)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image C_est_Chouette · Aug 26, 2020 at 06:49 PM 0
Share

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

avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges