This question was
closed Aug 19, 2020 at 08:01 PM by
rickmu05 for the following reason:
The question is answered, right answer was accepted
Successive AddForce calls additive?
Been chasing my tail on this for days and hoping someone can help -- In my game I want the wedge object to move towards the reticle using addforce2d.impulse.
The first call always works perfectly, but the second call seems to split the difference between the two calls even though I've zeroed out the velocity.
private void Update()
{
if (isBeingHeld == true)
{
Vector2 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
reticle.transform.position = new Vector2(mousePos.x, mousePos.y);
GameObject newLineGen = Instantiate(lineGeneratorPrefab);
LineRenderer lRend = newLineGen.GetComponent<LineRenderer>();
lRend.SetPosition(0, Wedge.transform.position);
lRend.SetPosition(1, reticle.transform.position);
Destroy(newLineGen, .025f);
moveNow = false;
}
if (moveNow == false)
{
//Rotate toward reticle
Vector2 direction = reticle.transform.position - rbc.transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
rbc.transform.rotation = rotation;
rbc.velocity = Vector2.zero;
}
if (moveNow == true)
{
//move on position of reticle
Vector2 target = reticle.transform.position;
float step = speed * Time.deltaTime;
rbc.AddForce(reticle.transform.position * step, ForceMode2D.Impulse);
// kill velocity if button clicked before hitting anything
if (buttonFire == true)
{
rbc.velocity = Vector2.zero;
buttonFire = false;
}
// stop when at reticle
var distance = Vector2.Distance(reticle.transform.position, rbc.transform.position);
if (distance <= .5f)
{
rbc.velocity = Vector2.zero;
moveNow = false;
}
}
}
Am I totally getting addforce wrong? Any help would be greatly appreciated!!!!
example.png
(432.9 kB)
Comment