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?
Your answer
Follow this Question
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