Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 cowmilk9 · Mar 12, 2019 at 04:52 AM · 3dcamera followcamera movement

Trouble with an Ocarina of Time-styled camera

I've challenged myself with coming up with a camera that should act akin to the camera from Zelda: Ocarina of Time. I've gotten basic movement, more or less, but now I've hit a wall and I can't seem to find a good answer after searching and trying different methods for hours. The movement itself works how I want it to except my camera keeps jittering. Here's my current code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CamBehavior : MonoBehaviour
 {
     [SerializeField]
     private GameObject player;              // Reference to player object
     [SerializeField]
     private float heightModifier = 1.5f;    // Height offset of camera relative to player
     [SerializeField]
     private float maxDistance = 10.0f;      // Max distance between player/camera before camera moves towards player
     [SerializeField]
     private float minDistance = 7.0f;       // Minimum distance between player/camera before camera moves away from player
 
     private float camDistance;              // Distance between player and camera
     private PlayerMovement playerMoveScript;    // Reference to the movement script of player
     private Vector3 targetPosition;         // Next target for camera to move towards
 
     public float GetCamDistance
     {
         get
         {
             return camDistance;
         }
     }
 
     void Awake()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         if (player == null)
         {
             Debug.LogError("ERROR: Player GameObject variable not defined.");
         }
 
         playerMoveScript = player.GetComponent<PlayerMovement>();
         transform.position = new Vector3(player.transform.position.x, player.transform.position.y + heightModifier, player.transform.position.z - maxDistance); // Set initial camera position
     }
 
     void LateUpdate()
     {
         targetPosition = new Vector3(player.transform.position.x, player.transform.position.y + heightModifier, player.transform.position.z);
         camDistance = Vector3.Distance(player.transform.position, transform.position);
 
         // Camera movement
         if (playerMoveScript.IsMoving && camDistance >= maxDistance)
         {
             transform.position = Vector3.MoveTowards(transform.position, targetPosition, playerMoveScript.Speed * Time.deltaTime);
         }
         else if (playerMoveScript.IsMoving && camDistance <= minDistance)
         {
             transform.position = Vector3.MoveTowards(transform.position, targetPosition, -playerMoveScript.Speed * Time.deltaTime);
         }
         transform.LookAt(player.transform);
     }
 }
 

After some testing, I figured it could be one of two things (or a combination of the two): Either it's the transform.LookAt that's causing the jitter somehow, or the if statements because the player might be constantly moving in and out of the limits I've set. Any and all help would be highly appreciated because I've been scratching my head at how to solve this logic puzzle for days now.

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 xxmariofer · Mar 12, 2019 at 08:30 AM 0
Share

can you share a gif? for knowing when is jittering

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

119 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

Related Questions

My camera doesn't follow my player while performing animations,my camera seperates from my character when it performs animations 0 Answers

FPS cam for Roll -a-Ball like game 1 Answer

Camera leaning problems (Lerping) 0 Answers

Jittering Camera 3D ,Jittery 3D camera using a LookAt() on an object 0 Answers

Camera move & rotate along vehicle 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges