• 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 YusufAfacan · 6 days ago · physicsrigidbodyaddforceclamp

How to remove addforce effect

Hi i make a throw basketball mechanic. The ball shouldn't go higher than 2.5f on y axis after it is throwed and hit the ground. I accomplish this kind of but ball hits the ground first time after it is throwed then bounces and stays(like hanged on air) in the 2.5f on y axis for a short period(like half to one second) Here is my code:

using System.Collections; using System.Collections.Generic; using UnityEngine; using System;

public class Ball : MonoBehaviour { public bool needClamp;

 public float forwardForce;
 public float upwardForce;
 public bool isShoot;
 public Rigidbody rb;

 private Vector2 fingerDownPos;
 private Vector2 fingerUpPos;

 public bool detectSwipeAfterRelease = false;

 public float SWIPE_THRESHOLD = 20f;



 // Start is called before the first frame update
 void Start()
 {
     Clamp();
 }

 // Update is called once per frame
 void Update()
 {

     if (needClamp && isShoot)
     {
         rb.velocity = Vector3.zero;
         rb.angularVelocity = Vector3.zero;
     }

     Vector3 position = transform.position;

     position.y = Mathf.Clamp(position.y, 0, 10f);

     if (needClamp)
     {
         position.y = Mathf.Clamp(position.y, 0, 2.5f);
     }
     
     transform.position = position;

     foreach (Touch touch in Input.touches)
     {
         if (touch.phase == TouchPhase.Began)
         {
             fingerUpPos = touch.position;
             fingerDownPos = touch.position;
         }

         //Detects Swipe while finger is still moving on screen
         if (touch.phase == TouchPhase.Moved)
         {
             if (!detectSwipeAfterRelease)
             {
                 fingerDownPos = touch.position;
                 DetectSwipe();
             }
         }

         //Detects swipe after finger is released from screen
         if (touch.phase == TouchPhase.Ended)
         {
             fingerDownPos = touch.position;
             DetectSwipe();
         }
     }


 }

 void Clamp()
 {
     needClamp = true;
 }

 void DetectSwipe()
 {

     if (VerticalMoveValue() > SWIPE_THRESHOLD && VerticalMoveValue() > HorizontalMoveValue())
     {
         Debug.Log("Vertical Swipe Detected!");
         if (fingerDownPos.y - fingerUpPos.y > 0)
         {
             OnSwipeUp();
         }
         else if (fingerDownPos.y - fingerUpPos.y < 0)
         {
             OnSwipeDown();
         }
         fingerUpPos = fingerDownPos;

     }
     else if (HorizontalMoveValue() > SWIPE_THRESHOLD && HorizontalMoveValue() > VerticalMoveValue())
     {
         Debug.Log("Horizontal Swipe Detected!");
         if (fingerDownPos.x - fingerUpPos.x > 0)
         {
             OnSwipeRight();
         }
         else if (fingerDownPos.x - fingerUpPos.x < 0)
         {
             OnSwipeLeft();
         }
         fingerUpPos = fingerDownPos;

     }
     else
     {
         Debug.Log("No Swipe Detected!");
     }
 }

 float VerticalMoveValue()
 {
     return Mathf.Abs(fingerDownPos.y - fingerUpPos.y);
 }

 float HorizontalMoveValue()
 {
     return Mathf.Abs(fingerDownPos.x - fingerUpPos.x);
 }

 void OnSwipeUp()
 {
     Shoot();
 }

 void Shoot()
 {
     if (!isShoot)
     {
         rb.AddForce(transform.forward * forwardForce, ForceMode.Impulse);
         rb.AddForce(transform.up * upwardForce, ForceMode.Impulse);
         isShoot = true;
         needClamp = false;
         
     }
 }

 private void OnCollisionEnter(Collision collision)
 {
     if (isShoot && collision.gameObject.CompareTag("Ground"))
     {
         needClamp = true;

         

         if (transform.position.y <= 2.5f)
         {
             isShoot = false;
         }
     }
 }

 void OnSwipeDown()
 {
     //Do something when swiped down
 }

 void OnSwipeLeft()
 {
     //Do something when swiped left
 }

 void OnSwipeRight()
 {
     //Do something when swiped right
 }

}

What should i fix? Could you help me please?

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

299 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 get objects in front of the player to be blasted away? 1 Answer

Ball Speed is not increasing as per code 0 Answers

AddForce in Coroutine 1 Answer

Script makes plane point in general direction but doesn't point fully... read description please! 1 Answer

How to change the direction of a rigidbody without adding to its speed? 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