Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by MagMaffin · Apr 03, 2020 at 01:55 PM · forcetrajectoryshotgolfpower

Force to the ball trajectory

Hello, I would like to add a force to my ball in my golf game, so if the player clicks the ball and holds a left mouse button, the power of the shot is growing, so is the trajectory. When the power reaches a maximum value the ball is shot immediately. I have a huge problem with it, and I'm stuck on it for a few days now. Maybe someone can help me? Code below: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Ball : MonoBehaviour
 {
     internal Rigidbody2D ballRB;
     internal Vector3 startPos = Vector3.zero;
 
     int dotCount;
 
     internal bool isClicked = false;
     bool trajectoryVisible = false;
     bool paraboleReachedEndOfScreen = true;
 
     GameObject trajectoryDots;
     GameObject ballClick;
 
     //TrajectoryDots
     List<Transform> dots = new List<Transform>();
 
     //shot velocity
     Vector2 shotForce = Vector2.zero;
 
     public float startValShootingPower = 3f;
     public float shootingPower = 3f;
 
    
     float minBallPointerDist = 0.5f;
 
     float dotSeparation = 10f;
 
     float dotShift = 5f;
 
     Transform lastDotTransform;
 
     void Start()
     {
         startPos = transform.position;
         startValShootingPower = shootingPower;
 
         ballRB = GetComponent<Rigidbody2D>();
         trajectoryDots = GameObject.Find("Trajectory Dots");
         ballClick = transform.Find("Ball Click Area").gameObject;
 
         dotCount = trajectoryDots.transform.childCount;
         foreach (var dot in trajectoryDots.GetComponentsInChildren<Transform>())
         {
             dots.Add(dot);
         }
         dots.Remove(trajectoryDots.transform);
     }
 
     void Update()
     {
         foreach (var dot in dots)
         {
             if (dot.GetComponent<SpriteRenderer>().enabled)
             {
                 lastDotTransform = dot;
             }
         }
         paraboleReachedEndOfScreen = lastDotTransform.position.x <= -UsefulReferences.CameraViewFrustum.x / 2 || lastDotTransform.position.x >= UsefulReferences.CameraViewFrustum.x / 2;
 
         ballClick.SetActive(!isClicked);
 
         RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
         if (Input.GetButtonDown("Fire1"))
         {
             if (hit.collider != null)
             {
                 //check if the mouse is hovering ball click area
                 if (hit.collider.transform.root.name == name)
                 {
                     trajectoryVisible = true;
                 }
             }
         }
 
         if ((Input.GetButtonUp("Fire1") || (paraboleReachedEndOfScreen && Input.GetButton("Fire1"))) && !isClicked && trajectoryVisible) //player released the mouse button or the parabole reached the end of the screen
         {
             trajectoryVisible = false;
             isClicked = true;
             UsefulReferences.ballScript.ballRB.constraints = RigidbodyConstraints2D.FreezeRotation;
             ballRB.velocity = shotForce;
             foreach (var dot in dots)
                 dot.transform.position = Vector3.zero;
             Invoke("GroundCheckAfterShot", 0.1f);
         }
 
         if (trajectoryVisible && !isClicked)
         {
             shotForce = (transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition)) * shootingPower;
             for (int i = 0; i < dotCount; i++) 
             {
                 dots[i].gameObject.SetActive(true);
 
                 dots[i].transform.position = new Vector2(transform.position.x + shotForce.x * Time.fixedDeltaTime * (dotSeparation * i + dotShift),
                     transform.position.y + shotForce.y * Time.fixedDeltaTime * (dotSeparation * i + dotShift) - (-Physics2D.gravity.y / 2f * Time.fixedDeltaTime * Time.fixedDeltaTime * (dotSeparation * i + dotShift) * (dotSeparation * i + dotShift)));
             }
         }
         else
         {
             foreach (var dot in dots)
             {
                 dot.gameObject.SetActive(false);
             }
         }
         //if the ball tried to fly out of the screen
         if (transform.position.y > UsefulReferences.CameraViewFrustum.y / 2 || transform.position.x > UsefulReferences.CameraViewFrustum.x / 2 || transform.position.x < -UsefulReferences.CameraViewFrustum.x / 2)
         {
             OnCollisionEnter2D(null);
         }
 
 
         if (isClicked && UsefulReferences.ballScript.ballRB.constraints == RigidbodyConstraints2D.FreezeAll)
         {
             UsefulReferences.gcsScript.gameOver = true;
         }
     }
 
     void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.tag == "Hole")
         {
             UsefulReferences.gcsScript.Score();
             ballRB.constraints = RigidbodyConstraints2D.FreezeAll;
         }
     }
 
     void OnCollisionEnter2D(Collision2D collision)
     {
         ballRB.constraints = RigidbodyConstraints2D.FreezeAll;
     }
 
     void GroundCheckAfterShot()
     {
         if (transform.position.y - startPos.y < 0.05f)
             ballRB.constraints = RigidbodyConstraints2D.FreezeAll;
     }
 }
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

200 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 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

How do I use click and drag to change the power of my object 0 Answers

Calculate force needed to reach certain point (AddForce) 1 Answer

How do I make AddForce apply force relative to the rotation of another object? 0 Answers

Player sometimes don't stop moving when Key is release, and is properly installed in the fixedUpdate() 1 Answer

How to apply a force at certain point of an object? 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