Home > OS >  I want to push a ball in the direction of a target
I want to push a ball in the direction of a target

Time:05-17

Shoot(); should add some force to the ball and push in direction of an object "pointTarget", but when Shoot(); work it adds force in -x direction. How I can fix this, if you show I will be so grateful Sorry for my English

private void Shoot()
{
    var heading = pointTarget.position - transform.position;
    var distance = heading.normalized;
    go_ball.GetComponent<Rigidbody>().AddForce(distance * ShootForce, ForceMode.VelocityChange);
}

CodePudding user response:

heading.normalized does not give you distance, it gives you direction vector. You have to use magnitude to get the distance. Since the distance is float, the multiplication problem is also solved:

var distance = heading.magnitude;

CodePudding user response:

I think your direction is incorrect, it should start with the rigidbody itself.

var heading = pointTarget.position - go_ball.transform.position;
  • Related