Tekken Fighting game Camera
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;
}
}
Any help if you can?
Answer by maki47 · Jan 25, 2016 at 04:11 PM
For FUTURE READERS I got it! Just use the distance from the center to the location of one of the objects, and multiplied it. Use some margin and limit its zooming
Vector3 cameraNewPos = mCenterTrans.position - (rotation * offset * (distanceOfCharacters/2f) * margin);
cameraNewPos.x = Mathf.Clamp (cameraNewPos.x, minZoom, maxZoom);
mTransform.position = cameraNewPos;
Answer by thomas1231 · May 14, 2017 at 01:35 AM
Just wanted to ask did you make a controller that work like Tekken and if so how
Your answer
Follow this Question
Related Questions
How to hide and show level based on player/camera position? 0 Answers
Character quickly vibrates when reaching specific objects. 0 Answers
Camera Shaking At High Speed 0 Answers
How do I make my camera Switch between 2 layer views? 0 Answers
Can i do an app in "2D" that changes in VR mode when i want? 0 Answers