• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ShamusO · Apr 08, 2020 at 10:18 PM · cameracamera rotatethird-personflyingship

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 :

OLD Code Results

 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);


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vipertex13 · Apr 09, 2020 at 12:30 AM 0
Share

@ShamusO

Hello!

Do you want the camera to follow the player from behind?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
     }
 }
 


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 }
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShamusO · Apr 11, 2020 at 05:06 PM 0
Share

Why do you do the actions in FixedUpdate rather than LateUpdate?

avatar image Vipertex13 ShamusO · Apr 11, 2020 at 10:16 PM 0
Share

Just preference

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

189 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I only rotated X, but Y and Z are shifting as well when I play the game. 1 Answer

Player rotation = Camera Rotation 0 Answers

Refencial change axes 0 Answers

Rotate camera a set amount of degrees around Player 0 Answers

Starting position gyroscope camera unity 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges