Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Space Flight, Camera Issues

Discussion in 'Scripting' started by FleshKnot, Jan 27, 2015.

  1. FleshKnot

    FleshKnot

    Joined:
    May 4, 2014
    Posts:
    67
    Hello everyone. So I've put together a c# code to allow my camera to follow my spaceship without forcing it to be restrained in one spot behind the ship so that it adds dept, it's very similar to the camera as seen in Star Fox. The only problem that I'm having with it is that when I do a sharp turn on my ship when playing the camera seems to lose track of the ship. It's happening on the X Y and Z axis. I've linked a video demonstrating the issue. I will also link my codes. Any and all help is appreciated. Thank you. Also, if you like the code and would like to use it please feel free.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFollowXY : MonoBehaviour
    5. {
    6.     public Transform objectToFollow;
    7.     public Vector2 movementRatio = Vector2.one;
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.      
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void LateUpdate ()
    17.     {
    18.         Vector3 newPosition = objectToFollow.position;
    19.         newPosition.x *= movementRatio.x;
    20.         newPosition.y *= movementRatio.y;
    21.         newPosition.z = transform.position.z;
    22.         transform.position = newPosition;
    23.     }
    24. }
    25.  
    Essentially with the script I have an empty game object parented to the player and than I parent the camera to the player as well, then I apply the script to the camera and select the empty game object .

    Here is the link to the video demonstrating the issue
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This should be in scripting ...
     
  3. FleshKnot

    FleshKnot

    Joined:
    May 4, 2014
    Posts:
    67
    I decided it should be in discussion because it could relate to non scripting issues. If an administrator feels that it should be moved then all is well.
     
  4. Deon-Cadme

    Deon-Cadme

    Joined:
    Sep 10, 2013
    Posts:
    288
    This is definitely a code issue.

    You need to adjust the cameras Z value when you travel in the opposite direction. The camera ended up traveling in front of the ship.

    Will you have levels with turns as well? Maybe you need to write a more advanced camera that takes the ships rotation into account? Just manipulating the transform can result in choppy movement or warping. It could be worth to look into linear interpolated movement.

    ps. video reminded me of some really cool arcade games that I had forgotten :D
     
  5. FleshKnot

    FleshKnot

    Joined:
    May 4, 2014
    Posts:
    67
    Alright so I revised my script to account for the rotation of the game object when being controlled but it's completely wacked and is not even usable. It's taking the game camera, rotating it 180 degrees and pulls the camera way out of view of the player object. Have any idea why it's doing that?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFollowXY : MonoBehaviour
    5. {
    6.     public Transform objectToFollow;
    7.     public Vector2 movementRatio = Vector2.one;
    8.     public Quaternion movementRatio2 = Quaternion.Euler(0,30,0);
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.      
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void LateUpdate ()
    18.     {
    19.         Vector3 newPosition = objectToFollow.position;
    20.         Quaternion newRotation = objectToFollow.rotation;
    21.  
    22.         newPosition.x *= movementRatio.x;
    23.         newRotation.x *= movementRatio2.x;
    24.         newPosition.y *= movementRatio.y;
    25.         newRotation.y *= movementRatio2.y;
    26.         newPosition.z = transform.position.z;
    27.         newRotation.z *= movementRatio2.z;
    28.         transform.position = newPosition;
    29.         transform.rotation = newRotation;
    30.     }
    31. }
    32.  
    Lines 7, 20, 23, 25, 27 and 29 are supposedly accounting for the rotation
     
  6. Deon-Cadme

    Deon-Cadme

    Joined:
    Sep 10, 2013
    Posts:
    288
    @GreyMynx - Early morning writing so excuse any strange choice of words or sentences... haven't refilled my black blood yet :confused:

    There are two ways to make objects in general follow your ship; either parent them to the ship or calculate (what you are attempting now) rotate and lerp it. Both got pro:s and con:s depending on your requirements and current skill.

    The first is like towing a car, or your ship will tow your camera. Just remember that you got global transforms and local transforms now.
    The second is like Jar Jar Bink... Jar Jar follows the jedis wherever they go. It can be used in more advanced ways but it might give you the same feeling that Jar Jar gives you while talking.

    Maybe these links will help you in the right direction:


    (Yes, I am a cruel person)
     
    FleshKnot likes this.