I am currently working on a ball game and I want to rotate the camera around the sphere ( the object that I am using as a ball). I have successfully done moving the camera along with the ball but I am not able to rotate the camera along with the ball. Please help me.
2 Answers
2One way is to use Transform.RotateAround().
var target : Transform;
var speed : float = 20.0; // Degrees per second;
function Update()
{
transform.RotateAround(target.position, Vector3.up, speed * Time.deltaTime);
}
This rotates around the Y axis at 20 degrees per second.
You are right. But what I want is that the Camera should rotate proportional to the rotation of the sphere (ball). How can I achieve this?
– Murtaza-LearnerRereading everything, I think you want is a child relationship between the camera and the sphere. Just position the camera to have the correct view, and drag and drop it on top of the sphere in the Hierearchy.
– robertbuI wanted something like this :
if(transform.rigidbody.velocity.magnitude < 50){
transform.rigidbody.AddForce(targetCamera.right * Input.GetAxis("Horizontal") * 15);
transform.rigidbody.AddForce(targetCamera.forward * Input.GetAxis("Vertical") * 15);
}
if(Input.GetButton("Jump")){
transform.position.y = transform.position.y + jumpHeight * Time.deltaTime;
}
//how far do you want camera to be placed on the x-axis
xpos = -10 * targetCamera.forward.x;
//how far do you want camera to be placed on the z-axis
zpos = -10 * targetCamera.forward.z;
targetCamera.position = Vector3.Lerp(targetCamera.position, (transform.position+Vector3(xpos, 3, zpos)), 0.05);
spotLight.position.y = targetCamera.position.y;
spotLight.position.x = transform.position.x;
spotLight.position.z = transform.position.z;
targetCamera.LookAt(transform);
I got this code working.
Along which axis is the camera to be rotated? Also what triggers the rotation?
– VivekDI want to rotate the camera along the x-axis. I have put a box collider. As soon as the ball hits the box collider I want camera to rotate. The box collider is set to isKinematic.
– Murtaza-Learner