- Home /
Applying a constant force to a ball along the camera axis.
Hi, I have this ball I'm trying to apply a constant force to when the W key is pressed. I have a camera which follows the ball and always looks at it. The camera can be moved around the ball by using the mouse (similar to how you can move the camera around a world of warcraft character). I'd like to apply the force to the ball in the direction the camera is looking ( again similar to how you would move a character forward in world of warcraft). This is the code I've wrote to do it.
var forwardValue = 15;
function Update ()
{
//Forward
if (Input.GetKey ('w'))
{
var cam : Transform = Camera.current.transform;
var cameraRelativeForward : Vector3 = cam.TransformDirection (Vector3.forward);
constantForce.force = Vector3(constantForce.force.x, constantForce.force.y,
cameraRelativeForward.z * forwardValue);
}
//Stationary
else if(Input.GetKeyUp ('w'))
{
constantForce.force =(cameraRelativeForward * 0);
}
}
The problem is the force only moves the ball along the global z axis and it only applies the maximum force when the camera's z axis is aligned with the global z axis. If the z axis of the camera is aligned with the global x axis the ball doesn't move at all.
If I change this
constantForce.force = Vector3(constantForce.force.x, constantForce.force.y,
cameraRelativeForward.z * forwardValue);
to this
constantForce.force = (cameraRelativeForward * forwardValue);
it has the desired effect but it presents another problem in that later I would like to apply a constant force along the camera's x axis to the ball, but this above code sets the constant for for all 3 axis and so the two forces would be conflicting with each other.
Thank you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180728im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Add wind effect to Game Object 1 Answer
what is the comand for global torque using constant force 1 Answer
rotating constantly 1 Answer
Constant Running on x axis 0 Answers
keep my collider on my particle system close together 1 Answer