Confused with Quaternion.AngleAxis, 45° player's forward ?

Hi,
I am confused with Quaternion.AngleAxis, I try to rotate by 45 degrees compared to the player’s forward, and the result is never good. Would you know how to make this work? (the world’s right is the player’s forward)

void shoot(float p = 0){
    float power = p;
    if (power == 0){
        power = Mathf.Min(Time.time - timeStart, 0.04f);
    }
    f = power * 50000 * Time.deltaTime;
  
    //HERE
    Quaternion fAngle = Quaternion.AngleAxis(45, ptr.forward);
    Vector3 force = fAngle.ToEulerAngles() * f;
    stopForce();
    rb.AddForce(force);
}

Thanks

I am not sure what you are applying force for. Anyway this will rotate your player 45 degrees:

transform.rotation = Quaternion.AngleAxis(45, Vector3.up);

Thanks, I would like the player to run forward, collides with the ball, if the angle is “45”, then the ball should go 45° , added to the player’s forward (the force is the amount of time I let the key down).
So when I use Vector3.up, the ball goes up in the air (it should stay on the x/z axis).
If I try some values, from around 45 to 179 with Vector3.forward, it is almost every time the same direction towards the world Vector3.forward, and after 180, it goes straight at the opposite at the bottom of the screen.
If I try with “ptr.forward”, it goes towards the Vector3.forward and in the air.

How can the ball takes an angle of ptr.forward + 45°, multiplied by the force?

Do you want the ball to bounce off the player only if it was hit on a 45 degree angle?

What is the significance of 45 degrees?

I am sorry but I am still not sure what you are trying to do.

:slight_smile: I want the player to run with the ball, so if the joystick moves " 45° ", the player rotates 45 degrees, so the ball should also move by 45 degrees. I ask the ball to add a force in a direction of : player.transform.forward + 45°. But it does not work.

I don’t think you have to add the 45 degrees. You should be able to add force in the forward direction of the player.

Add this to your player’s script in the update method for debugging:

Debug.DrawRay(transform.position, transform.forward * 100, Color.magenta);

This script I put on a sphere:

    public Transform Player;

    public float Force = 10;

    private CharacterController _PlayerController;
    private Rigidbody _RigidBody;
   
    void Start ()
    {
        _RigidBody = GetComponent<Rigidbody>();

        Player = GameObject.FindGameObjectWithTag("Player").transform;
        _PlayerController = GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterController>();

    }

    void Update ()
    {
        if(_PlayerController.velocity != Vector3.zero)
        _RigidBody.AddForce(Player.transform.forward * Force, ForceMode.Force);
    }

You will have to tweak it a bit for your purposes. Notice that in my case, the force will not be applied unless the character has moved. To check that, I am using a character controller. If you are moving your player with a rigidbody then you can check like this

if(_PlayerRigidbody.IsSleeping() == false)
        _RigidBody.AddForce(Player.transform.forward * Force, ForceMode.Force);

Thanks, I appreciate your help, but it confuses me a bit with what I want. I did not explain well, here is the thing :
the ball is hit and only then the player should rotate with a smooth effect. When you play soccer, you can hit the ball and then only rotate your body, you don’t rotate all of a sudden.

So I should add the angle of the joystick to the ball. The player should rotate after the ball has taken its new angle, I cannot use the “new rotation” of the player because it will occur after the ball has been hit. (the smooth effect takes a small amount of time).

It is all good.

You are making a soccer game?

Is the ball only going to be hit on a 45 degree angle? Because if there are other angles, then hard-coding 45 degrees is not a practical approach.