I get an error: Operator * cannot be applied to operands by type Vector3 and Vector3
_rb.AddForce(movement * Camera.main.transform.forward * speed * Time.fixedDeltaTime, ForceMode.Impulse);
I get an error: Operator * cannot be applied to operands by type Vector3 and Vector3
_rb.AddForce(movement * Camera.main.transform.forward * speed * Time.fixedDeltaTime, ForceMode.Impulse);
Movement and Camera.main.transform.forward is two Vector3. You cannot multiple two Vector3 together. What you want to do is express the movement in the space of the camera. If I’m not mistaken, you will need to use _rb.AddForce(camera.transform.InverseTransformDirection(movement) * speed, ForceMode.Impulse).
There is 4 things that you should consider also:
void Update () {
if(Input. GetButtonDown(“fus”)){
crate. rigidbody. AddForce(cameraRelativeRight * power);
crate. rigidbody. AddForce(cameraRelativeForward * power);
}
}
Caregiver Connect
You can’t multiply two vectors together as you do it with two floats. Vectors can be combined in various ways, like with Vector3.Scale, Vector3.Cross or Vector3.Dot.
Also, usually it’s easier to control a character with a Character Controller component. But, of course, it depends on what you’re trying to achieve in your game.
Anyways, here’s a simpler way to do it.
Vector3 cameraSpaceMovement = Camera.main.TransformDirection (movement);
_rb.AddForce (speed * Time.fixedDeltaTime * cameraSpaceMovement), ForceMode.Impulse);
Note: for good performances, it’s always better to write the vector at the end of the series of the operands to multiply, otherwise it’ll multiply each axis for each operation in between.