- Home /
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);
}
}
}
Could you please post screenshots of your lighting. It's hard to tell how your lighting work.
Ok this does look weird. What material does your player have? What kind of light source are you using and where is it located?
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.
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.
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
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.
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);
}
}
}