Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 micheal_s · Jun 20, 2019 at 08:43 PM · cameracamera-movement

How to achieve an undertale style camera

I'm looking for 2D camera, that will allow the player to move a certain radius before following him, like in undertale. i have written some code which does this but it jitters a bit and doesnt seem like a good way of going about it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraFollowPlayer : MonoBehaviour
 {
     public GameOb`enter code here`ject followTarget;
     private float cameraZ = 0;
 
     private Camera camera;
 
     public float cameraDistanceThreshold = 4f;
 
     public float speed = 4;
 
     
 
     void Start()
     {
         cameraZ = transform.position.z;
         camera = GetComponent<Camera>();
     }
 
     void FixedUpdate()
     {
         CheckForThreshold();
     }
 
     private void CheckForThreshold()
     {
         if (Vector2.Distance(followTarget.transform.position, transform.position) > cameraDistanceThreshold)
         {
             Vector3 delta = followTarget.transform.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, cameraZ));
 
             Vector3 destination = transform.position + delta;
 
             destination.z = cameraZ;
 
             transform.position = Vector3.MoveTowards(transform.position, destination, speed*Time.deltaTime);
         }
     }
 
 }

I have looked everywhere. and for the love of god i cant find any clean solution. i will be extremely appreciative for any help i can get :)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hellium · Jun 20, 2019 at 09:17 PM

I've never played Undertale, but give the following script a shot:

 using UnityEngine;
 
 [RequireComponent(typeof(Camera))]
 public class CameraFollowPlayer : MonoBehaviour
 {
     public Transform Target;
     public float MaxDistance = 4f;
     public float Speed = 4;
 
     private new Camera camera;
     private Vector3 viewportPoint;
     private Vector3 desiredPosition;
 
     void Start()
     {
         camera = GetComponent<Camera>();
         viewportPoint = new Vector3( 0.5f, 0.5f, Mathf.Abs( transform.position.z - Target.position.z ) );
         desiredPosition = transform.position;
     }
 
     void LateUpdate()
     {
         FollowTarget();
     }
 
     private void FollowTarget()
     {
         Vector3 centerPoint = camera.ViewportToWorldPoint( viewportPoint );
         Vector3 deltaPosition = Target.position - centerPoint;
 
         if ( deltaPosition.sqrMagnitude > MaxDistance * MaxDistance )
             desiredPosition = transform.position + deltaPosition;
 
         transform.position = Vector3.MoveTowards( transform.position, desiredPosition, Speed * Time.deltaTime );
     }
 
 }
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 micheal_s · Jun 22, 2019 at 06:30 PM 0
Share

Thanks for the help, however it still jitters :/ ill try to look around

avatar image Hellium micheal_s · Jun 24, 2019 at 07:16 AM 0
Share

Do you have an heavy scene? Is your character managed by the Physics engine?

If so, you may need to use coroutines for this script. (Following script not tested)

  public Rigidbody Target;

  void Start()
  {
      camera = GetComponent<Camera>();
      viewportPoint = new Vector3( 0.5f, 0.5f, $$anonymous$$athf.Abs( transform.position.z - Target.position.z ) );
      desiredPosition = transform.position;
      StartCoroutine( FollowTarget() ) ;
  }
 
  private IEnumerator FollowTarget()
  {
      WaitForFixedUpdate wait = new WaitForFixedUpdate();
      while( true )
      {
          Vector3 centerPoint = camera.ViewportToWorldPoint( viewportPoint );
          Vector3 deltaPosition = Target.position - centerPoint;
 
          if ( deltaPosition.sqr$$anonymous$$agnitude > $$anonymous$$axDistance * $$anonymous$$axDistance )
              desiredPosition = transform.position + deltaPosition;
 
          transform.position = Vector3.$$anonymous$$oveTowards( transform.position, desiredPosition, Speed * Time.deltaTime );
          yield return wait;
     }
  }
 

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

174 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

Related Questions

Vuforia camera frustum out of view after image target is lost 0 Answers

I can not find Cinemachine in package manager. There is no option of 'All Assets in package manager.' 1 Answer

Camera movement stuttery with interpolate on,Camera movement with interpolate on? 0 Answers

Make camera follow player and align direction 0 Answers

Camera shaking and parent movement 1 Answer


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