- Home /
Third Person Space Shooter Camera (Rogue Squadron Like Camera)
Update! :: I have to change the script and it still doesn't work exactly as I'd like. You can see a video of the results HERE. One thing I find strange is that I define up as the ships y, but the camera is still not rotating on its z (when the ships local y is obviously moving). I have asked on Stack Exchange game Dev and a Unity Facebook group, but no good solutions. PLEASE HELP! Here is the updated code::
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewCameraScript : MonoBehaviour {
[SerializeField] private GameObject _000;
[SerializeField] private GameObject _001;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void LateUpdate() {
_000.transform.position = _001.transform.position + ((_001.transform.forward * -10f) + (_001.transform.up * 2f));
_000.transform.LookAt(_001.transform.localPosition);
Quaternion _wantedRotation = Quaternion.LookRotation(_001.transform.forward, _001.transform.up);
}
}
Hey, All so I am making a game with a plane/ship and I am having issues with the camera. The camera currently follows the position of the ship, and rotation until I apply rotation in the Z (plane rolling). I am attempting to get a camera similar to Rogue Squadron for N64. I have posted code on the camera and I have uploaded a video if yall would like to see how it works currently. Basically, the camera currently follows the ship but I can rotate the ship to fly toward the camera and out of frame. One solution that came pretty close was having the player movement script applied to the main camera and having the ship as a child of the main camera. For the most part, this worked though the movement was very direct / spot on. Thank you for your time and help.
Video of the current implementation :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camaraScript : MonoBehaviour {
[SerializeField] private GameObject _item000;
[SerializeField] private GameObject _item001;
[SerializeField] private float _xAdd;
[SerializeField] private float _yAdd;
[SerializeField] private float _zAdd;
private void Start()
{
}
// Update is called once per frame
void Update () {
//Camera.main.transform.position =
_item000.transform.position = new Vector3(_item001.transform.position.x + _xAdd, _item001.transform.position.y + _yAdd, _item001.transform.position.z + _zAdd);
// Let the camera look at the player
_item000.transform.LookAt(_item001.transform);
Answer by ShamusO · Apr 09, 2020 at 04:07 AM
Yes, I would like the camera to follow the player from behind. The code I have provided works until I rotate the plane in the Z which is necessary for my flight controller. That is the same for most of the "follower cameras" they will work if you just have to follow the player on the ground, but not in this more freeform flight situation. Like I stated previously if I make the ship a child of the camera and have the controls affect the camera I get essentially what I want though I would like to do this through code and not have to parent objects. Also when I have set everything the same as described in the code above and set the camera rotation to the p[lane rotation that doesn't work either (understandably though it seemed like a good idea at the time). Thank you for any help.
Answer by ShamusO · Apr 11, 2020 at 02:18 AM
Finally resolved this, used input from people all over. Including this youtube video Youtube Video. Big Issue Is I wasn't thinking about other ways to use the transform.lookat / quaternian.lookat creatively.Thank you, everyone, for all of your help!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NewCameraScript : MonoBehaviour {
[SerializeField] private GameObject _000;
[SerializeField] private GameObject _001;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void LateUpdate() {
_000.transform.position = _001.transform.position + ((_001.transform.forward * -10f) + (_001.transform.up * 2f));
Quaternion _wantedRotation = Quaternion.LookRotation(_001.transform.forward, _001.transform.up);
_000.transform.rotation = _wantedRotation;
}
}
Answer by Vipertex13 · Apr 11, 2020 at 06:21 AM
Here is my code to follow an object you can change where the camera looks thou
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollowController : MonoBehaviour
{
void start ()
{
followSpeed = 10;
lookSpeed = 75;
}
public void LookAtTarget()
{
Vector3 lookDirection = objectToFollow.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(lookDirection, Vector3.up);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, lookSpeed * Time.deltaTime);
}
public void MoveToTarget()
{
Vector3 targertPos = objectToFollow.position +
objectToFollow.forward * offset.z +
objectToFollow.right * offset.x +
objectToFollow.up * offset.y;
transform.position = Vector3.Lerp(transform.position, targertPos, followSpeed * Time.deltaTime);
}
private void FixedUpdate()
{
LookAtTarget();
MoveToTarget();
}
public Transform objectToFollow;
public Vector3 offset;
public float followSpeed;
public float lookSpeed;
}
Why do you do the actions in FixedUpdate rather than LateUpdate?