0
votes

I have a Vector3 that indicates the input force of the player to the ball

Vector3(moveHorizontally, 0.0f, moveVertically);

I have another Vector3 that indicates the normalized direction of a plane

Vector3(-1.0f, 0.0f, 0.0f);

How can I make the ball follow the direction of the object using AddForce()? In this case, I think the ball should go left instead of front if I'm not mistaking.

EDIT: I need no move a ball forward in a certain direction. Each time the ball triggers a plane, the direction of the plane is facing becomes the direction of the ball. It's a game where the ball rolls on a non-linear track with turns.

2
You will have to explain and show more of your environment. Can you make a minimal reproducible example of the part which implements the ball movement and the plance? Then we could propose how to implement the desired effect.Yunnosch
Simply trigger it with a collider, read plane direction and pass the direction as a parameter into AddForce.Dave

2 Answers

1
votes

I would not use AddForce since I guess you want to change the direction immediately when colliding.

I think what you mean is something like

private rigidBody;

private void Awake()
{
    rigidBody = GetComponent<RigidBody>();
}

private void OnCollisionEnter(Collision col)
{
    // Or however you want to check with what you are colliding
    if(col.gameObject.tag != "Plane") return;

    var hitPlane = col.gameObject;
    var invertedPlaneNormal = hitPlane.transform.forward.normalized * -1;

    var currentSpeed = rigidBody.velocity.magnitude;

    // Keep same velocity but change direction immediately
    rigidBody.velocity = invertedPlaneNormal * currentSpeed;
}

you could also add damping like

[Range(0,1)]
public float dampFactor;

//...

rigidBody.velocity = invertedPlaneNormal * currentSpeed * (1 - dampFactor);
0
votes

I do not have the specific code, but assuming that the ball has a rigidbody attached to it, and both, the path and the ball have colliders, you can use Unity collision events to determine the future direction of the ball.

Let's say the ball has a script attached to it and when it collides it will trigger an event onCollisionEnter, then from there you can get the script component of a plane which it has collided with and apply the force in the right direction as a response to the collision.

Note that onCollisionEnter only occurs when the object enters the collision.