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 Nikhira · Nov 15, 2015 at 10:01 AM · camera followcamera movementmario

Camera Follow problem D:

I've been trying to make the camera work properly for some time now and i can't find anything useful. I want to make a Super Mario-like camera follow, I'm using this code, and it's working nicely for the x position but i wanted the threshold for y to be bigger, and i already tried to make one for y and one for x, because i dont want the player to be in the center of the camera, i only want the camera to go up or down in case the player goes really really close to the borders for y position... i even tried to use the script in an object and make it follow the player and child the camera to the object and it didnt go out as planned... depending on the speed the camera stutters too so if theres somethin i can do to make it smoother please help me! so basically i need a way to limit the y movement according to the player position where the player is never on the center of the camera and something to make the camera follow smoother.

 public class CameraController : MonoBehaviour
 {
 
     [SerializeField]
     Transform player;
 
     private Vector3 moveTemp;
     [SerializeField]
     float speed = 4;
     [SerializeField]
     float xDifference;
     [SerializeField]
     float yDifference;
     [SerializeField]
     float threshold = 3;
 
     
     
 
     void FixedUpdate()
     {
         if (player.transform.position.x > transform.position.x) {
             xDifference = player.transform.position.x - transform.position.x;
         } else {
             xDifference = transform.position.x - player.transform.position.x;
         }
 
         if (player.transform.position.y > transform.position.y) {
             yDifference = player.transform.position.y - transform.position.y;
         } else {
             yDifference = transform.position.y - player.transform.position.y;
         }
 
         if (xDifference >= threshold || yDifference >= threshold)
         {
             moveTemp = player.transform.position;
             moveTemp.z = -17;
             transform.position = Vector3.MoveTowards(transform.position, moveTemp, speed * Time.deltaTime);
         }
 
        
     }
 
 
 }

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
Best Answer

Answer by Dinosaurs · Nov 16, 2015 at 02:29 AM

To fix the stuttering, you might want to move this code into LateUpdate() instead of FixedUpdate(). FixedUpdate should only be used for physics related calculations.

As for clamping the Y value, when you set the transform position you could try setting the Y value using Mathf.Clamp + the starting Y value to keep it within a certain range of the starting Y.

Comment
Add comment · Show 1 · 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 Nikhira · Nov 16, 2015 at 11:07 AM 0
Share

Wow thanks!! i had no idea about LateUpdate, i'm kinda new to C#. it totally solved my problem, now it goes smooth no matter whats the camera speed :DD also for the clamping i did that to everything but it wasnt really what I wanted because its according to world position, but i figured i can child an invisible object above the player and make the camera follow it xD so thank you! that really helped man! @Dinosaurs

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Player movement relative to the camera 4 Answers

Rotate camera a set amount of degrees around Player 0 Answers

Trouble with an Ocarina of Time-styled camera 0 Answers

How would I go about making a camera system for a game akin to the starfighter mode of Battlefront 2? 1 Answer

How to Smooth Camera Follow script for Android? 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