Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 lightningman2222 · Jun 19, 2015 at 03:56 AM · camera2dlightingplatformerflip

Unity 2D camera and lighting issues

So I have this code used for my character to move left and right and also jump. I also made it so that when I moves left, the sprite flips to face the left direction. One big issue though is that when I make the camera the player's child to make it follow the player, the camera flips completely to the other side when turning left instead of staying on one side. And when doing lighting effect, the light only effects the player when moving right, but not when moving left. Is there a way I can make the light impact the player while facing both left and right, while also having the camera not constantly flip sides when turning when it's the player's child object?

Thank you

My code is here:

  using UnityEngine;
  using System.Collections;
  
  public class smallbot_movement : MonoBehaviour {
  
      public float moveSpeed;
      public float jumpHeight;
  
      void Update(){
  
          Movement ();
      }
  
      void Movement()
      {
          if(Input.GetKey (KeyCode.D))
          {
              transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
              transform.eulerAngles = new Vector2(moveSpeed, 0);
          }
          if(Input.GetKey (KeyCode.A))
          {
              transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
              transform.eulerAngles = new Vector2(moveSpeed, 180);
          }
  
          if(Input.GetKeyDown (KeyCode.Space))
          {
              GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight);
          }
      }
  }
  
Comment
Add comment · Show 4
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 xeetsh · Jun 20, 2015 at 11:54 PM 0
Share

Could you please post screenshots of your lighting. It's hard to tell how your lighting work.

avatar image lightningman2222 · Jun 21, 2015 at 09:31 PM 0
Share

alt text

alt text

unity-6-21-2015-5-29-30-pm-191.png (12.6 kB)
unity-6-21-2015-5-29-36-pm-851.png (10.9 kB)
avatar image xeetsh · Jun 21, 2015 at 10:02 PM 0
Share

Ok this does look weird. What material does your player have? What kind of light source are you using and where is it located?

avatar image lightningman2222 · Jun 21, 2015 at 11:00 PM 0
Share

I made a new material, and set the shader to sprite/diffuse. I'm also using a pointlight and it was placed above and very very slightly right to the player's head. It seems to never effect the player at all when he faces left.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by xeetsh · Jun 21, 2015 at 11:29 AM

It also is possible make the camera a child of the player without the camera rotation with the player. But there is a much easier way. Make the camera a single object and write a script that set's the camera transform.position to the player.transform.position. With this you also have the huge advantage that you can let the camera stop following the player if you want to at any time.

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 lightningman2222 · Jun 22, 2015 at 01:07 AM 0
Share

I'm new to coding and I don't know how to implement it. Sorry to ask, but how to I set the camera's transform position to the player's?

Again, thank you

avatar image
0

Answer by felixpk · Jun 21, 2015 at 09:43 PM

To flip the player just scale on the X-Axis:

 Vector3 scale = transform.localScale;
 
 scale.x *= -1;
 
 transform.localScale = scale;

This is not tested, but I think it should work. I made games like this and never had lightning issues, hope it helps.

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 lightningman2222 · Jun 22, 2015 at 01:05 AM 0
Share

I tried it and it caused my player's sprite to spaz out when moving and caused the player to only move right and now even left.

I added in the code like this:

 using UnityEngine;
 using System.Collections;
 
 public class smallbot_movement : $$anonymous$$onoBehaviour {
 
     public float moveSpeed;
     public float jumpHeight;
 
     void Update(){
 
         $$anonymous$$ovement ();
     }
 
     void $$anonymous$$ovement()
     {
         if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D))
         {
             transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
             Vector3 scale = transform.localScale;
             
             scale.x *= 1;
             
             transform.localScale = scale;
         }
         if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A))
         {
             transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
             Vector3 scale = transform.localScale;
             
             scale.x *= -1;
             
             transform.localScale = scale;
         }
 
         if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space))
         {
             GetComponent<Rigidbody2D>().velocity = new Vector3(0, jumpHeight);
         }
     }
 }
 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Lighting looks really weird in 2D game 1 Answer

ScreenToWorldPoint not accurate 0 Answers

Cinemachine camera weird movement on Windows build 0 Answers

2.5D Platformer Camera follow character Y axis but not for every jump? 1 Answer

2d camera 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