Hi, I’m working on this 3d fighting game, it’s almost a tekken clone. Now I need to code the camera movement. I already have the code for the camera movement, it always lookAt the side of two characters. But my problem is I can’t make it zoom Out/in as the player moves away from each other. I tried a lot of solution found in the internet but nothing solves it or related to it. Players move SSBB like in tekken, all scripts I found was for game whose characters are only moving on X-axis. Here’s my code:
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour {
Transform player1;
Transform player2;
Transform mTransform;
Transform mCenterTrans;
public GameObject centerPoint;
public Vector3 cameraOffset;
public float height;
float marginx = 3f;
Vector3 center;
Vector3 offset;
void Start () {
mCenterTrans = centerPoint.transform;
mTransform = transform;
player1 = Player.player1.mCharacter.mModel.transform;
player2 = Player.player2.mCharacter.mModel.transform;
centerPoint.transform.position = centerOfChars(player1.position,player2.position);
offset = centerPoint.transform.position - transform.position;
}
void LateUpdate () {
updatePosition ();
}
void updatePosition()
{
//calculate distance of players
float distanceOfCharacters = Vector3.Distance (player1.position, player2.position);
//update centerPoint position
mCenterTrans.position = centerOfChars(player1.position,player2.position);
mCenterTrans.LookAt (player2.position);
//calculate for nexPosition of Camera
float angle = (mCenterTrans.eulerAngles.y);
float anglez = (mCenterTrans.eulerAngles.z);
Quaternion rotation = Quaternion.Euler (0, angle,0);
mTransform.position = mCenterTrans.position - (rotation * offset);
center = mCenterTrans.position;
//add height to camera
center.y += height;
mTransform.LookAt (center);
}
Vector3 centerOfChars(Vector3 pCharPos1,Vector3 pCharPos2)
{
return (player1.position + player2.position) * 0.5f;
}
}
someMultiplier is just going to be some number you make up and plug different values into until it feels right - my guess is probably in the neighborhood of 1.5 - 2.0, assuming your camera has the standard 60 degree FOV.
@StarManta , can you help me more regarding this? I’m now stuck, I need to limit it’s zooming. I have made some solution but still cause some bugs. specially if I zoom out. Here’s what my code now: