Unity Mobile Joystick and Screen not following player

Hello,

So there is 2 parts to this issue… 1. the joystick, I am not sure if its controlling the player/character/object right, I do see it moving vertical and horizontal when moving the joystick… However, when moving the joystick trying to get to move straight up, down, left or right… it never does, it like zig zags or at an angle and never straight down etc… not sure if maybe my code is not right?

below is the joystick code:

    public CharacterController2D controller;
    public Joystick joyStick;
    public float moveSpeed = 40.0f;
    public float fastSpeed = 80.0f;

    float horizontalMove = 0.0f;
    float verticalMove = 0.0f;

    public void Update()
    {
        ////horizontal
        if (joyStick.Horizontal <= 0.3f && joyStick.Horizontal > 0.0f)
        {
            horizontalMove = moveSpeed;
        }
        else if (joyStick.Horizontal >= 0.4f)
        {
            horizontalMove = fastSpeed;
        }
        else if (joyStick.Horizontal >= -0.3f && joyStick.Horizontal < 0.0f)
        {
            horizontalMove = -moveSpeed;
        }
        else if (joyStick.Horizontal <= -0.4f)
        {
            horizontalMove = -moveSpeed;
        }
        else
        {
            horizontalMove = 0f;
        }

        //vertical
        if (joyStick.Vertical <= 0.3f && joyStick.Vertical > 0.0f)
        {
            verticalMove = moveSpeed;
        }
        else if (joyStick.Vertical >= 0.4f)
        {
            verticalMove = fastSpeed;
        }
        else if (joyStick.Vertical >= -0.3f && joyStick.Vertical < 0.0f)
        {
            verticalMove = -moveSpeed;
        }
        else if (joyStick.Vertical <= -0.4f)
        {
            verticalMove = -moveSpeed;
        }
        else
        {
            verticalMove = 0f;
        }

        Player_Character.transform.position = transform.position + new Vector3(horizontalMove * Time.deltaTime, verticalMove * Time.deltaTime);
    }

so the 2nd issue, I am trying to get the camera to follow the object, however for some reason, its not… its still stays the same place, not sure if its my Hierarchy or code… but below is the cameraMovement code, and screenshot is the Hierarchy of the objects, the Main is canvas, then below that or child of that is the panel… with the panel having everything…

Code below of the camera:

Code (CSharp):

public class CameraFollow : MonoBehaviour
{
    public Transform target;
    public Vector3 offset;

    // Update is called once per frame
    void Update()
    {
        this.transform.position = target.position + offset;
    }
}

the screenshot is attached of how i have it Hierarchy… thinking of mobile and also using canvas as one of the main parents to control or display the resolution 1920 x 1080…

hope this helps which I hope we can solve this. image is called Heirarchy_game.png

7607272--944728--Heirarchy_game.PNG

Find out if the joystick values are what you expect first. Use Debug.Log() liberally.

Also, don’t scatter noise like this:

in 57 places all over your code. It absolutely boggles the eyes.

Instead, copy it down to a single variable, like h and v, and use that only.

I also have no idea what you’re doing with those if/else statements. Simplify those a lot.

If you’re just gating discrete stop/slow/fast motions, combine the inputs in a Vector2 and use the total magnitude to decide speed. Deciding on axis-independent basis is probably going to do something weird.

There’s one lousy line of code there. Print what values are going through it, find out why it’s not what you expect.

Beyond that, camera stuff is pretty tricky… it might be best to use Cinemachine from the Unity Package Manager.

Hey Kurt, big thanks for the info, your right, all that code… ugly… but now take a look :slight_smile:

        input = new Vector3(joyStick.Horizontal, joyStick.Vertical);
        if((input.x <= 0.40f && input.x >= -0.40f) && (input.y <= 0.40f && input.y >= -0.40f))
        {
            Vector3 velocity = input.normalized * moveSpeed;
            transform.position += velocity * Time.deltaTime;
        }
        else if((input.x > 0.40f || input.x < -0.40f) || (input.y > 0.40f || input.y < -0.40f))
        {
            Vector3 velocity = input.normalized * fastSpeed;
            transform.position += velocity * Time.deltaTime;
        }

much cleaner :slight_smile:

so i am doing an if elseif statement to show running, so if the person pushes the joystick further down or more on x/y axis, it will make the player run, else walk or dont…

As for the camera, I am still going to work on it, and let you know if Cinemachine works or not.

How about:

if (input.magnitude >= 0.6f)  // fast?
{
  Vector3 velocity = input.normalized * fastSpeed;
  transform.position += velocity * Time.deltaTime;
}
else if (input.magnitude >= 0.3f)  // slow?
{
  Vector3 velocity = input.normalized * moveSpeed;
  transform.position += velocity * Time.deltaTime;
}

Awesome even better thank you :slight_smile:

sadly still dont know, i tried to use Cinemachine, and still wont follow the player, it seems to stay with the canvas… attached is the hierarchy…

so in short, I have the scene
Main
Then Canvas
Canvas for the background and stuff
canvas for the players, the controls/UI, and some text.

When I attach the script that says “CameraFollow”, to the Canvas with the player info, it just drops from the screen along with the player and controls… doesnt even stay, just drops… weird, never seen that, I dont have rigid body attached the anything or the player object… I do have charactercontroller2d, BUT, I set the gravity to 0… so it should stay right???

sorry, here is an up-to-date Hierarchy, that I am trying to simplify to getting it working… but cant :frowning: I want the camera view to move with the player, but never does, the camera aka the canvas stays, I do see the “Main Camera” move, but I dont actually see anything move, just the character thats in the center move when I move the joystick.

7612345--945841--Heirarchy_game3.PNG