- Home /
TPS Camera roll ball
Hi everyone,
I'm currently trying to make a rolling ball 'platform' game where you have to finish the route to win.
It looks like this:
Let's me explain how it's working. You can move the camera horizontally by pushing left and right button, you will rotate around the ball. There is the code :
transform.RotateAround(target.transform.position, (cammovement), Time.deltaTime *smooth);
To move on the ball will follow the camera direction by adding a force when you use top and down button. Here it is (with rbT as the target Rigidbody):
void RollBall()
{
//set force down camera's look direction
forceDirection = transform.forward;
//remove y force direction for angled camera
forceDirection = new Vector3(forceDirection.x, 0, forceDirection.z);
if(Input.GetAxis("Vertical") != 0)
{
//add force to ball
rbT.AddForce(forceDirection.normalized * speed * (Input.GetAxis("Vertical")));
}
}
My first problem was that the camera doesn't follow the ball even it still rotating around. I experimented that we cannot use transform.position and transform.RotateAround in the same code/GameObject.
So I decided to put the Camera inside an Empty GameObject and force the GO to follow the ball using a classic transform position + offset and it's worked pretty well.
void LateUpdate() { transform.position = player.transform.position + offset; }
But now I'm trying to manage the collision between the camera and the terrain. I have found a tutorial which explains that easily and I implemented it in, but of course, it contains a transform.position and it's not working on the GO parent (which is not rotating).
There is the experimentation code:
void Update()
{
if(Input.GetAxis("Vertical") != 0)
{
desiredCameraPosition = transform.TransformPoint(maxDistance * (Cam.transform.forward * Input.GetAxis("Vertical")));
}
//desiredCameraPosition = transform.TransformPoint(dollyDir * maxDistance);
RaycastHit hit;
Debug.DrawLine(Cam.transform.position, desiredCameraPosition, Color.green);
if (Physics.Linecast(Cam.transform.position, desiredCameraPosition, out hit))
{
Debug.Log("1");
distance = Mathf.Clamp((hit.distance*0.7f), minDistance, maxDistance);
}
else
{
Debug.Log("2");
distance = maxDistance;
}
Cam.transform.position = Vector3.Lerp(Cam.transform.localPosition, dollyDir * distance, Time.deltaTime * smooth);
}
So that is my question. How can I manage the camera rotation and using the transformation position to avoid passing thought the terrain and still be able to rotate around my ball?
Your answer
![](https://koobas.hobune.stream/wayback/20220612223844im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
roll a ball with rotating camera script. when i go back the ball fly 0 Answers
How to make a 3D camera rotate relative to the world? 1 Answer
Screen is 2 cameras with each different properties. 1 Answer
How can i make the camera look up and down through keyboard keys? 2 Answers
Jerky 3rd Person Camera Following Movement and Rotation 0 Answers