playMaker

Author Topic: Making a CAMERA for a 3D figthing game ?  (Read 1896 times)

NinjaX

  • Full Member
  • ***
  • Posts: 108
  • XCO is the ULTIMATE IOS Ninja!!
    • View Profile
    • XCOGAMES
Making a CAMERA for a 3D figthing game ?
« on: June 06, 2015, 12:11:00 PM »
Does anyone have any tips for me on how to create a Camera for a 3D fighter ?

Ill take anything :)


So I posted this question in UNITYs forums and I got a response from a very nice gentleman who seems like he really knew whats up, And I was wondering if someone can help me interpret what he said in PlayMaker Talk... I do understand what he is saying but I wouldn't mind having some input from PM community :D

Here is hes response:

Create a script with a reference to both characters transforms and then finds the correct position every frame (in the Update method)

Lets assume the character references are called charA and charB.

First find the average position of the two characters (the position between them).
Vector3 middle = (charA.transform.position + charB.transform.position) / 2f;

Now find a direction perpendicular to the vector between the characters and also perpendicular to "up".
Vector3 betweenVector = charB.transform.position - charA.transform.position;
Vector3 perpendicular = Vector3.Cross(betweenVector, Vector3.up);

Move the "middle" position out along the perpendicular vector, and a bit up aswell
Vector3 targetCamPosition = middle + perpendicular * distance + Vector3.up * upDistance;

Now, to make things a bit smooth, don't move the camera directly to that position every frame, instead Lerp towards it.
camTransform.position = Vector3.Lerp(camTransform.position, targetCamPosition, Time.deltaTime * snappyness);

Finally, make the camera look at "middle"
camTransform.LookAt(middle);

Here's an implementation of this I whipped together:

using UnityEngine;
using System.Collections;

public class Fighter3DCamera : MonoBehaviour
{
public GameObject charA, charB;
public float distance = 1f;
public float upDistance = 1f;
public float snappyness = 5f;

public Transform camTransform;

// Update is called once per frame
void Update ()
{
Vector3 middle = (charA.transform.position + charB.transform.position) / 2f;

Vector3 betweenVector = charB.transform.position - charA.transform.position;
Vector3 perpendicular = Vector3.Cross(betweenVector, Vector3.up);

Vector3 targetCamPosition = middle + perpendicular * distance + Vector3.up * upDistance;

camTransform.position = Vector3.Lerp(camTransform.position, targetCamPosition, Time.deltaTime * snappyness);
camTransform.LookAt(middle);
}
}
« Last Edit: June 06, 2015, 12:16:07 PM by NinjaX »
XCO is the ULTIMATE IOS Ninja, Beware for he sees Everything and Nothing all at once!

dudebxl

  • Hero Member
  • *****
  • Posts: 602
    • View Profile
Re: Making a CAMERA for a 3D figthing game ?
« Reply #1 on: June 06, 2015, 05:00:34 PM »
Hey,

I hope you don't mind but I converted (to an action), modified, adapted and added some features to the script you posted as it is actually a very cool camera effect (tested with two angry cubes). ;D

you can find it here: http://hutonggames.com/playmakerforum/index.php?topic=10495.0

My feedback: If that is the movement you are looking then run with it.  :P
« Last Edit: June 06, 2015, 05:09:59 PM by dudebxl »

NinjaX

  • Full Member
  • ***
  • Posts: 108
  • XCO is the ULTIMATE IOS Ninja!!
    • View Profile
    • XCOGAMES
Re: Making a CAMERA for a 3D figthing game ?
« Reply #2 on: June 06, 2015, 08:47:54 PM »
Mind ? No no at all dude :D Thanx bro...
XCO is the ULTIMATE IOS Ninja, Beware for he sees Everything and Nothing all at once!