Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 JackB226 · Apr 15, 2020 at 05:05 PM · speedshoothitrigidbody.addforcegolf

Rigidbody2D Problems With Golf Game

I am making a mini golf game where you drag in the opposite direction you want to hit the ball and depending on how far back you drag your mouse, that's how hard you hit.

This is the code for the shooting:

 if (this.GetComponent<Rigidbody2D>().velocity.magnitude <= 0.1f && inHole == false)
         {
             ready = true;
         } else
         {
             ready = false;
             //Debug.Log(this.GetComponent<Rigidbody2D>().velocity.magnitude.ToString());
         }
 
         if (Input.GetMouseButtonDown(0) == true && !aiming && ready)
         {
             aiming = true;
         }
 
         if (targetGoal != null)
         {
             this.transform.position = Vector2.Lerp(this.transform.position, targetGoal.transform.position, 2f);
         }
 
         if (aiming)
         {
             prevPos.transform.position = this.transform.position;
             //ENABLE LINE
             line.GetComponent<LineRenderer>().enabled = true;
             //CONTROL INPUT && SHOOT POSITIONS
             startPos = this.transform.position;
             line.GetComponent<LineRenderer>().SetPosition(0, startPos);
             Vector3 shootPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             shootPos.z = 0;
             shootPos = this.transform.position + (this.transform.position - shootPos);
             endPos = shootPos;
 
             if (Vector3.Distance(startPos, shootPos) > maxDist)
             {
                 Vector3 dir = endPos - startPos;
                 endPos = this.transform.position + (dir.normalized * maxDist);
             }
             line.GetComponent<LineRenderer>().SetPosition(1, endPos);
         } else
         {
             line.GetComponent<LineRenderer>().enabled = false;
         }
 
         if (Input.GetMouseButtonUp(0) == true && aiming && ready)
         {
             endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Shoot();
         }
 
 
 
 private void Shoot()
     {
         aiming = false;
         //Debug.Log("Shooting @" + endPos.ToString());
         if(Vector2.Distance(startPos, endPos) > maxDist)
         {
 
         }
         Vector2 direction = startPos - endPos;
         this.GetComponent<Rigidbody2D>().AddForce(direction * speed * Time.deltaTime);
         score++;
         overallScore++;
         overallScoreKeeper.text = overallScore.ToString();
         scoreKeeper.text = score.ToString();
 
     }


My problem is that the speed that I shoot drastically changes to make the ball shoot forward instead of going at the speed that I set it at random times. Sometimes it goes way to fast and sometimes way to slow. I don't know why it does this.

Any help would be nice. Thank you.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

126 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

Related Questions

Hit enemy life 0 Answers

Unity Raycast Script, Help! 1 Answer

How can I do an enemy damage with different parts of the body? 4 Answers

Get inpact location OnCollisionEnter? (c#) 2 Answers

Rigidbody Problems 1 Answer


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