Camera SmoothFollow rolling ball problem

I want to let my camera follow a rolling ball with the SmoothFollow script.

When i set the target of the SmoothFollow script to the ball the camera begins rotating because the ball is rotating.
I have made an empty GameObject as a child of the ball with this script on it and let the camera follow them:

function Update () {
	transform.eulerAngles = Vector3(0,0,0);
}

This does also not work because the camera does not know the rotation of the ball.
The other idea was to made an empty GameObject with this script on it to follow the ball and let the camera follow this GameObject:

var Ball : GameObject;

function Update () {
	transform.position = Ball.transform.position;
}

But then the camera does also not know the direction of the rolling ball.

Can someone help?

hey I don’t know much about it cz I am trying to do it myself but perhaps you can see how the first person controller script works and try to imitate it it’s a script that you can import it’s in the assets called character controller.
good luck.

Did you do your rolling as animation, or programmatically? When I did my ball rolling game the basic smooth camera worked fine. I did all my rolling as animation though.

The control of the ball is not the problem. I add force to the rigidbody to let the ball roll:

function FixedUpdate () {
    rigidbody.AddForce (Vector3.right * 250);
    
    if (Input.touchCount == 1){
    var touch = Input.touches[0];
    if (touch.position.x < Screen.width/2){
        rigidbody.AddForce (Vector3.forward * 1000);
    }
    else if (touch.position.x > Screen.width/2){
        rigidbody.AddForce (Vector3.forward * -1000);
    }
}
}

try making an empty game object and making the ball a child of that, instead of the other way around, and have the ball’s movement script attached to the ball. think of it like this.

you have a box.
you have a ball.
the ball is inside the box.
the camera looks at the box.
the ball rotates inside the box, completely unaffecting the camera.

Edit: or wait that wouldn’t work >.< lol. ok try a different camera script maybe? I actually can’t think of a good solution lol :frowning:

something like this might work?

using UnityEngine;
using System.Collections;

public void CameraScript : Monobehaviour {
	public int x = 1;
	public int y = 1;
	public int z = 1;
	
	public Transform target;

	public void Update(){
		UpdateCameraPosition();
	}
	
	private void UpdateCameraPosition(){
	
		transform.position = new Vector3(transform.position.x - x,
										transform.position.y - y,
										transform.position.z - z);
	
	}

}

I’m still kinda new-ish to both Unity3D’s scripting and C# in general but I think that might work something like what you want? if you’re rotating a ball then that shouldn’t shoot the camera every which way, and you can move the ball forward backward left right up down or whatever and the camera should follow.

also I just wrote that up real quick in Notepad++ so idk if it’d even work lol.

So, looking at this, I am thinking that you will need to look at the current velocity of the ball, and use it as a reference for direction.

Consider:

var height=5.0;
var distance=20.0;

var directionNormal=Vector3.Scale(target.rigidbody.velocity, Vector3(1,0,1)).normalized;
if(directionNormal==Vector3.zero)
	directionNormal=Vector3.Scale(transform.position - target.position, Vector3(1,0,1)).normalized
var wantedOffset=directionNormal * distance;
wantedOffset.y=height;
var wantedPosition=target.position + wantedOffset;

In this method, we take the current velocity of the ball and only use the X and Z, then normalize it. This gives us an actual direction where we want the camera to be at based off of the movement of the ball. If our movement is zero, we use the current camera offsets to calculate direction.

OK, we have a normal, we simply multiply that time the distance we want to be behind the ball, and add the height that we want to be above it. Our wanted position is now just the target position + that offset.

now we just do a Vector3.Lerp(transform.position, wantedPosition, damp * Time.deltaTime); and we get a new camera position based off the current movement of the ball.

1 Like