Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
1
Question by GDNova · Sep 18, 2014 at 02:39 PM · camera2dcamera-movementfollowtracking

Main Camera child under player, causes flip

I am building a 2d platformer, and I have my movement script complete; however I wanted the main camera to follow my character throughout the level, so I thought the best way to do this would be to place the camera as a child object under my player in the hierarchy. That way the camera moves alongside the player. This works alright except for in my player Movement script I use "transform.eulerAngles = new Vector2(0,180);" in order to flip the character when walking the other direction. When the camera is a child of the sprite, it flips the camera as well. Is there a way to do this without causing the camera to flip?

On a side note would it also be possible to make the camera follow the player, but only in the x or y planes?

Thanks for any help you can provide

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by 14ercooper · Aug 06, 2015 at 11:47 PM

This script will cause the camera to follow the player without rotating to the back when the player turns around. You might need to change with axis has the subtract 10 to make it display correctly. The subtraction amount (the 10 in the code) can be changed to adjust how close to the player the camera is, just make sure it is a positive integer.

 using UnityEngine;
 using System.Collections;
 
 public class CameraUpdater : MonoBehaviour {
 
     private Transform player;
     
     void Start () {
         player = GameObject.Find ("Player").transform;
     }
 
     void Update () {
         transform.position = new Vector3 (player.position.x, player.position.y, player.position.z - 10);
     }
 }

Edit: Forgot how to use it. Un-parent the main camera from the player and add this as a script.

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
0

Answer by zeeshandar · Sep 18, 2014 at 04:11 PM

I saw this code somewhere hope it helps.

     public Gameobject target;
     private float trackSpeed = 10;
     
     
     
     // Track target
     void LateUpdate() {
   
             float x = IncrementTowards(transform.position.x, target.transform.position.x, trackSpeed);
             float y = IncrementTowards(transform.position.y, target.transform.position.y, trackSpeed);
             transform.position = new Vector3(x,y, transform.position.z);
 
     }
     
     // Increase n towards target by speed
     private float IncrementTowards(float n, float target, float a) {
         if (n == target) {
             return n;    
         }
         else {
             float dir = Mathf.Sign(target - n); // must n be increased or decreased to get closer to target
             n += a * Time.deltaTime * dir;
             return (dir == Mathf.Sign(target-n))? n: target; // if n has now passed target then return target, otherwise return n
         }
     }


Just drag and drop the player object in the script..

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

Answer by Slev · Sep 18, 2014 at 03:30 PM

I assume the rotation is to handle the sprite direction? Rather than adjust the character's euler angles, simply flip the sprite's X component, that will render it in reverse. For movement it's likely safer to use absolute left/right, rather than relative right.

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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Camera Smooth follow, FixedUpdate and LateUpdate odd difference, help needed. 1 Answer

camera follow problem-unity 4.3 2d 1 Answer

Isometric - Camera follow an object when at the edge of the screen 1 Answer

Camera follow - rotating rigidbody 1 Answer

2D Diablo style 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