Screen Adjustment Problem

I’m getting a little frustrated over this particular scene:

What I wanted to do was delete a sprite and change it.

This is the current sprite (character) that I have:


I just want change this because I realized this is not the same “character” that I want. This scene is perfect for me (all objects adjusts with the screen), I just want to delete and replace the character.

But, When I tried to replace it this happened.

They are literally on the same layer. All behavior are deactivated because I just want him to just stay still. But, all other layers go all over the place. Other objects don’t adjust to the screenwindow anymore. I would like to ask for some help, please :frowning:

so in short its: in scene;
object not disappering;
goes all over the place;
frustrated;;

idk how to help you but this code can help :smile:

using UnityEngine;

public class PlayerController : MonoBehaviour

{

[SerializeField] Transform playerCamera = null;

[SerializeField] float mouseSensitivity = 3.5f;

[SerializeField] float walkSpeed = 10.0f;

[SerializeField] float runSpeed = 12.0f;

[SerializeField] float gravity = 9.81f;

[SerializeField] bool lockCursor = true;

[SerializeField] float jumpHeight = 3f;

CharacterController controller = null;

Vector2 currentDir = Vector2.zero;

Vector2 currentDirVelocity = Vector2.zero;

Vector2 currentMouseDelta = Vector2.zero;

Vector2 currentMouseDeltaVelocity = Vector2.zero;

float cameraPitch = 0.0f;

float velocityY = 0.0f;

void Start()

{

    controller = GetComponent<CharacterController>();

    if (lockCursor)

    {

        Cursor.lockState = CursorLockMode.Locked;

        Cursor.visible = false;

    }

}

void Update()

{

    UpdateMouseLook();

    UpdateMovement();

}

void UpdateMouseLook()

{

    Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

    currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, 0.03f);

    cameraPitch -= currentMouseDelta.y * mouseSensitivity;

    cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);

    playerCamera.localEulerAngles = Vector3.right * cameraPitch;

    transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);

}

void UpdateMovement()

{

    Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

    targetDir.Normalize();

    currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, 0.3f);

    if (controller.isGrounded)

        velocityY = 0.0f;

    velocityY += gravity * Time.deltaTime;

    Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocityY;

    controller.Move(velocity * Time.deltaTime);

    if ((Input.GetKey("left shift") || Input.GetKey("right shift")) && controller.isGrounded && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.DownArrow))

    {

        velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * runSpeed + Vector3.up * velocityY;

        controller.Move(velocity * Time.deltaTime);

    }

    if (Input.GetKeyDown(KeyCode.J) && controller.isGrounded)

    {

        Debug.Log("Now Jumping!!");

        velocityY = Mathf.Sqrt(jumpHeight * -2f * gravity);

    }

}

}

When you replaced the character did you delete the original sprite and make another one or did you keep the original sprite object and replace the animation? It would be helpful to see some events because it may be some calculation, positioning, camera, something was based on the original character.

Hello @Lucky-j this is the unreplaced character

sorry if it looks a little messy.

When I delete that or just change it’s “name” to the one I am replacing it with it just go brrrr like everything don’t adjust to screen anymore when I drag the screen/window size. The background goes to the side etc.

I didn’t change the animation I just intend to delete and replace the character.

You don’t have any camera following the old sprite?

I’ve tried disabling it at the beginning of the scene. But it didn’t work.

Well I just meant, was there something you were doing with the old sprite that you are not doing with the new sprite? Because there is some reason everything looks great with the old sprite and falls apart for the new sprite.

The old sprite has no animation whatsoever. It’s just on the screen. The new sprite has SmoothCameraBehavior, Topdown behavior, and Health. I’ve tried to disable everything except for Health. I placed the new sprite in the same layer. I don’t know what to do. Maybe I’ll just go through this again.

Sometimes the best thing to do is set it aside and work on another part of your logic for the time being. Otherwise you can get irritable and cranky and then are useless for getting any sort of work done. When you say disabled, you deleted the smooth camera behavior from the new sprites properties?

This is what I did

Ok, I just tried that same thing for the smooth camera in the sample project I am copying from yours and it does not work in the beginning of the scene. But it does work if you move it down to regular events. And I kept the top down movement working on mine, and it still does not off center everything.

Thank you so much @Lucky-j ! I will try this. For now maybe I need to take a rest. Man, this whole thing made my brain twist and turn.

Yeh it can be tricky working with cameras. For example if you want to follow your character, but your character is on a different layer than the background, it will look like your character is not moving unless you set up the background layer camera to follow him. With the smooth camera extension, I also noticed the “jitters” you described if the character was not moving. I assume the way to fix this would be to deactivate the smooth camera when the player is not moving and activate it when it is moving (or tinker around in the behavior settings I guess).

I think if all you want to do is let your player walk around the scene but only be in the viewport, you don’t really need a camera to follow him (or a camera on the background layer to follow him, if he is on a different layer). That is more for when your background is so big that the player can keep moving and revealing more of the background that was not visible. You can set up borders so the player can’t walk out of the boundary you want.

1 Like