Problems with my ball

Hi Guys,

I’ve got a game setup where I have a ball that moves with the camera.

The left analog stick moves the ball and the right rotates the camera.

The problem is that although the ball moves with the camera I have no idea on how to get it to rotate in the direction its moving?

Could someone please shine some light on this.

Thanks very much

Are you using the standard camerafollow script?
what may be easyer for this situation is to completely make your own camera script that has two variables that can be adjusted by the joystick, height and angle. Up and down on the joystick controls the height and left and right control the angle. Then you could move the camera by using some code like this:

var change : Vector3 = Quaternion.EulerAngles(0,angle,0) * Vector3(0,0,distance);
transform.position = ball.transform.position + change;
transform.position.y = ball.transform.position.y + height;
transform.LookAt(ball);

next add force to the ball according to the left joystick and the camera’s angle variable. You’ll need a vector2 variable that is the difference between the joysticks original position, and where the player’s finger is on the screen. Now convert that to a vector3, but make sure you switch around z and y so that your ball goes forward and back not up and down. Then multiply that Vector3 by Quaternion.EulerAngles(0,CameraScript.angle,0).
Now you have a Vector3 that you can plug into RigidBody.AddForce.

By the way, great title.

Thanks for your great reply,

I was hoping I could do something simpler than this though, would it be possible to apply an animation to the ball to make it rotate correctly that way?

Here is a copy of the project files to better explain what I mean:

Download

To make the ball rotate forward in the direction the camera is pointing, you need to turn it around the camera’s X axis. You can do this with transform.Rotate:-

transform.Rotate(Camera.main.transform.right, turnAngle);

To calculate the angle to rotate the ball, you need to know the ball’s radius and the distance moved (which you have probably already calculated in order to move the ball). Divide the distance by the radius to get the angle in radians and then multiply this by Mathf.Rad2Deg:-

var distance = speed * Time.deltaTime; // ...or whatever.
var turnAngle = distance / ballRadius * Mathf.Rad2Deg;
transform.Rotate(Camera.main.transform.right, turnAngle);

Thank you very much andeeee,

I’ve been trying this but I seem to get this error:

I think its because my distance is calculated from

cameraTransform.TransformDirection( Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) );

which uses a vector 3?

I think I may have misunderstood you here…

Do you want to rotate the camera around the ball and then push the ball in the direction the camera is going, or move the ball and have the camera follow along from one side?