Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Camera Follow Failure

Discussion in 'Scripting' started by MisterSkitz, Dec 16, 2018.

  1. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    p1 is the player game object.
    xOff && yOff is a controlable attribute inside of the inspector for when I need the camera to shake or something cool like that.

    If the player isn't jumping, I want the camera to follow its position. However, when the player jumps, I want the camera to stay level.

    You'll only need to look at the Y-axis.

    The code works fine without the else statement.

    Since the else statement was added, the entire screen goes blank except the background color on the camera. All GameObjects disappear.

    So how do I achieve what I'm trying to do here?

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (isFollow)
    4.         {
    5.  
    6.             if (Pmove.isJumping == false)
    7.             {
    8.                 transform.position = new Vector3(p1.transform.position.x + xOff, p1.transform.position.y + yOff,
    9.                                                                                                           transform.position.z);
    10.             }
    11.             else //if(Pmove.isJumping == true)
    12.             {
    13.  
    14.                 transform.position = new Vector3(p1.transform.position.x + xOff, transform.position.y + yOff,
    15.                                                                                                          transform.position.z);
    16.  
    17.             }
    18.  
    19.  
    20.         }
    21.     }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You wouldn't add the offset to the y position, I don't think. You are just keeping the same y position of the camera.
     
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    That's pretty much what the camera is doing. If you notice, if jump is true, it's just the camera's transform.position.y. Not the player's.

    The offsets align the camera where I want it during run time. If an event happens, I can alter the offsets to make the camera move the way I want it to. Also, it makes it where the player isn't standing dead center of the camera. I can also control the camera when the player gets a certain distance from the center and make it re-align. Generally, the offsets are not the issue here though, my friend.

    When isJumping is true, all game objects vanish until player hits the ground. So I'm wondering if I need a LateUpdate() function added. But I don't know much about that, so I'm hoping to get some good advice.

    Thanks for your reply though!
     
  4. stackdynamic

    stackdynamic

    Joined:
    Mar 18, 2018
    Posts:
    13
    transform.position will get the CURRENT position. If you look at line 14, you'll notice that each frame you are jumping (BTW use FixedUpdate() and not Update() as this is a movement/physics-esque thing) the camera's vertical position is set to its current position plus offset. In other words, you are giving your camera a vertical velocity, as you are adding to its vertical position every frame. To resolve this, simply store its initial vertical position as a float,
    and add that to offset.

    EDIT: Didn't read your previous post about what your offset variable is. Since the camera is already offset correctly when the player begins the jump (I'm assuming so at least) adding the offset again is actually unnecessary. Just store that initial value, and set vertical position to only that.
     
    MisterSkitz likes this.
  5. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I switched to FixedUpdate() as you advised and that makes a lot of sense. However, the code still does the same thing upon jump. Here's a peek at how my jump function works and how the isJumping static variable is initialized. The jump function is called in a FixedUpdate() on my PlayerMove (Pmove) script. Is jumping is passed into my camera follow script (CamFollow).

    Code (CSharp):
    1.  void Pjump()
    2.     {
    3.        
    4.         if (Input.GetKeyDown(KeyCode.Space) && onFloor==true
    5.             || Input.GetKeyDown(KeyCode.W) && onFloor==true)
    6.         {
    7.            
    8.            
    9.                 player.velocity = new Vector2(0, jump);
    10.                 onFloor = false;
    11.             isJumping = true;
    12.            
    13.  
    14.         }
    15.        
    16.     }
    So I'm curious to hear more about the float variable you're talking about. I would have thought that the tranform.position would be floating point by default.
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Yeah, but the camera's y position is the position it is in, which is offset from the player, so when you use it, it wouldn't have an offset added to it. Only when you use the player's position you add the offset. You are, in effect, doubling the offset.
     
    MisterSkitz likes this.
  7. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Ok, that makes perfect sense. I'll see if that works. Thanks!
     
  8. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I removed the offset from the Y-Axis and the code works perfectly. Lol I feel dumb now :p

    But I learned a little something, so for that, I thank you, amigo!


    Final Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CamFollow : MonoBehaviour
    6. {
    7.  
    8.  
    9.     private GameObject p1;
    10.  
    11.     private bool isFollow;
    12.  
    13.     [Header("Camera Offset Values")]
    14.     [SerializeField]
    15.     private float xOff;
    16.     [SerializeField]
    17.     private float yOff;
    18.  
    19.     void Start()
    20.     {
    21.         p1 = GameObject.FindWithTag("Player");
    22.         isFollow = true;
    23.  
    24.     }
    25.  
    26.     void FixedUpdate()
    27.     {
    28.         if (isFollow)
    29.         {
    30.  
    31.            if (Pmove.isJumping == false) // This code works fine
    32.           {
    33.                 transform.position = new Vector3(p1.transform.position.x + xOff, p1.transform.position.y + yOff,
    34.                                                                                                           transform.position.z);
    35.            }
    36.            
    37.            if(Pmove.isJumping == true) // This is where the problem is: Y-Axis
    38.             {
    39.  
    40.                 transform.position = new Vector3(p1.transform.position.x + xOff, transform.position.y,
    41.                                                                                                          transform.position.z);
    42.  
    43.             }
    44.            
    45.  
    46.         }
    47.     }
    48. }
    49.  
     
  9. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Glad it worked out.
     
    MisterSkitz likes this.