Question by
shookun · Aug 30, 2021 at 06:28 PM ·
transformaddforceforcerigidbody.addforce
How to AddForce up to a certain height and then stop?
I am trying to get an object to raise up when the player object is near, it works fine... but there is no limit on the AddForce transform.up.
So what happens is the object floats off and is never seen again!
How can I AddForce but stop the force being applied after it hits a max height?
TIA
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Flag : MonoBehaviour
{
public float forceMultitude = 200;
private Rigidbody rb;
public Vector3 maxHeight;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
//FIX: Flag raises and never comes back down or stops at a set height
private void Update()
{
if (Game.Instance.GolfBall == null)
return;
if ((Game.Instance.GolfBall.transform.position - rb.position).magnitude < 5.0f)
{
rb.AddForce(transform.up * forceMultitude * Time.deltaTime);
}
else
{
//Return to start position?
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Predicting max jump height of a force impulse 1 Answer
Convert transform.position to Force 0 Answers
How do I get objects in front of the player to be blasted away? 1 Answer
Addforce trajectory 0 Answers