• 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 /
This question was closed Oct 11, 2015 at 12:56 PM by xxluky for the following reason:

Other

avatar image
Question by xxluky · Oct 10, 2015 at 07:20 PM · 2d-platformersmoothfollow

How to make camera follow player smoothly on X axes but not on y axis?

Hi guys,

I have a camera script that follows my player smoothly using "transform.position = Vector3.Lerp..." all the time.

Now I put in the scene cannon gun that shoots my player realy fast upwards (only on y axes) and my camera is lazy to follow the player at this point. I have already tryed to increase follow speed or parent the camera to my player but the camera always jumps on the player - that is not what I want.

I want the camera to be parented smoothly to the moving object - to the player. Is it even possible? How should I solve this??

Or even better - to follow my player smoothly on X axes but to be instant on Y axes with smooth aim?

Now I have this (following on X is smooth but the camera jumps on Y axes when targets the player):

 float xPos = Mathf.Lerp(transform.position.x, player.transform.position.x, smoothSpeed * Time.deltaTime);
 transform.position = new Vector3(xPos, player.transform.position.y, -10);

Please help me...

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by DiegoSLTS · Oct 10, 2015 at 09:57 PM

I'm not sure I follow you... Do you want your camera to follow the player in both axis but smooth the camera movement in the x axis only? Or do you only want to follow the player in the X axis and let the y position of the camera untouched?

If it's the first one I think your code should work, so I'm guessing you want to do the second thing. In that case, why are you using the player y position when updating the camera's position? This should follow only in the X axis:

 transform.position = new Vector3(xPos, transform.position.y, -10);
Comment
xxluky
S_George

People who like this

2 Show 3 · 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 xxluky · Oct 11, 2015 at 08:57 AM 0
Share

Hi, no no. First of all I want my camera to aim my player - to be on the center position + offset of my player smoothly. Then I want my camera to smoohtly follow my player only on the X axes but not smoothly on the Y axes. When the player is fired by the cannon upwards, the camera is lazy to follow him.

It is important to smoothly aim my player because when I parent my camera to the player or increase the follow up speed, the camera suddenly jumps.

Here's my script:

 public GameObject player;
 public Vector3 offset;
 public float smoothSpeed;
 
 void Awake ()
 {
     transform.position = player.transform.position + offset;
     moveUp = player.transform.position.y;
 }
 
 void Update()
 {
 Vector3 moveCamera = new Vector3 (player.transform.position.x, moveUp);
 transform.position = Vector3.Lerp (transform.position, moveCamera + offset, smoothSpeed * Time.deltaTime);
 }
avatar image xxluky · Oct 11, 2015 at 12:56 PM 0
Share

Never mind. I just increased camera follow speed and changed Update to FixedUpdate for smoother following.

avatar image ktsakiris · Jan 20, 2018 at 09:21 PM 0
Share

or :
transform.position = new Vector3(player.transform.position.x, 0, -10);

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 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

Related Questions

Rigidbody2d getting stuck when moving horizontally at random points! Can't figure it out :( 1 Answer

Enemy facing moving direction. 0 Answers

animation script for 2D platformer issue 2 Answers

Question about Rays and why getPoint function 1 Answer

How can I flip only my 'Player' gameobject, and l leave it's child object alone? 2 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