1
votes

So I am moving the camera around as so

public static CameraController Instance{ set; get; }

    public Transform lookAt;
    public Transform camTransform;

    private Camera cam;

    public const float Y_ANGLE_MIN = 0.0f;
    public const float Y_ANGLE_MAX = 50.0f;
    public float distance = 10.0f;
    public float currentX = 0.0f;
    public float currentY = 0.0f;
    public float sensitivityX = 7.0f;
    public float sensitivityY = 7.0f;

    private void Start(){
        Instance = this;
        camTransform = transform;
        cam = Camera.main;
    }

    private void Update(){
        currentX += InputManager.SecondaryHorizontal ();
        currentY += InputManager.SecondaryVertical ();

        currentY = Mathf.Clamp (currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
    }

    private void LateUpdate(){
        Vector3 dir = new Vector3 (0, 0, -distance);
        Quaternion rotation = Quaternion.Euler(currentY,currentX,0);
        camTransform.position = lookAt.position + rotation * dir;
        camTransform.LookAt (lookAt.position);
        this.transform.position += Vector3.up;
    }

I am rotating my player around as the camera rotates as so

private void Update(){
        this.transform.eulerAngles = CameraController.Instance.camTransform.eulerAngles;
        }

I am expecting that when my player is rotated to the left that pressing the MainVertical() (attached to the WASD controls & joystick X,Y axis)

public static float MainVertical(){
        float r = 0.0f;
        r += Input.GetAxis ("J_MainVertical");
        r += Input.GetAxis ("K_MainVertical");
        return Mathf.Clamp (r, -1.0f, 1.0f);
    }

will rotate move the player forward in the direction that is now the forward direction

However the character continues to move forward in what was originally the forward direction.

I can only assume that the issue is though the forward axis has changed for the object the x,y,z in world space doesnt change and thats what MainVertical() is still accessing

As requested here is the code for my character movement

private CharacterController controller;
    private Animator anim;

    //Speed settings
    private float walkingSpeed = 5.0f;
    private float runningSpeed = 7.5f;

    //Jump and gravity settings
    private float verticalVelocity;
    private float gravity = 14.0f;
    private float jumpForce = 10.0f;

    private void Start(){
        controller = GetComponent<CharacterController> ();
        anim = GetComponent<Animator> ();
    }

    private void Update(){
        this.transform.eulerAngles = CameraController.Instance.camTransform.eulerAngles;
        Jump ();

        Vector3 moveVector = InputManager.MainJoystick ();
        moveVector.x = moveVector.x * walkingSpeed;
        moveVector.y = verticalVelocity;
        moveVector.z = moveVector.z * walkingSpeed;
        controller.Move (moveVector * Time.deltaTime);

        Vector3 camForward = Vector3.Scale (CameraController.Instance.camTransform.forward, new Vector3 (1, 0, 1)).normalized;


        if (moveVector.x != 0 || moveVector.z != 0) {
            anim.SetBool ("IsIdle", false);
            anim.SetBool ("IsWalking", true);
        } else {
            anim.SetBool ("IsIdle", true);
            anim.SetBool ("IsWalking", false);
        }
    }

    protected void Jump(){
        if (controller.isGrounded) {
            verticalVelocity = -gravity * Time.deltaTime;
            if (InputManager.AButton()) {
                anim.SetTrigger ("Jump");
                verticalVelocity = jumpForce;
            }
        } else {
            verticalVelocity -= gravity * Time.deltaTime;
            //anim.SetTrigger ("Landing");
        }
    }
}

And here is my input manager

    // -- Axis

    public static float MainHorizontal(){
        float r = 0.0f;
        r += Input.GetAxis ("J_MainHorizontal");
        r += Input.GetAxis ("K_MainHorizontal");
        return Mathf.Clamp (r, -1.0f, 1.0f);
    }

    public static float MainVertical(){
        float r = 0.0f;
        r += Input.GetAxis ("J_MainVertical");
        r += Input.GetAxis ("K_MainVertical");
        return Mathf.Clamp (r, -1.0f, 1.0f);
    }

    public static Vector3 MainJoystick(){
        return new Vector3 (MainHorizontal (), 0, MainVertical ());
    }

    public static float SecondaryHorizontal(){
        float r = 0.0f;
        r += Input.GetAxis ("J_SecondaryHorizontal");
        r += Input.GetAxis ("Mouse X");
        return Mathf.Clamp (r, -1.0f, 1.0f);
    }

    public static float SecondaryVertical(){
        float r = 0.0f;
        r += Input.GetAxis ("J_SecondaryVertical");
        r += Input.GetAxis ("Mouse Y");
        return Mathf.Clamp (r, -1.0f, 1.0f);
    }

    public static Vector3 SecondaryJoystick(){
        return new Vector3 (SecondaryHorizontal (), SecondaryVertical (), 0);
    }

    // -- Buttons

    public static bool AButton(){
        return Input.GetButtonDown ("A_Button");
    }

    public static bool BButton(){
        return Input.GetButtonDown ("B_Button");
    }

    public static bool XButton(){
        return Input.GetButtonDown ("X_Button");
    }

    public static bool YButton(){
        return Input.GetButtonDown ("Y_Button");
    }
1
can we see the code that moves the player forward and where MainVertical() is called?Jinjinov
updated question @JinJiForgedInFlame
i think you are cofusing this.transform from player and Input.GetAxis - they are 2 totally different things, unrelated to each other. imagine FIFA, where you have 11 soccer players, each has his own this.transform but you still have only 1 Input.GetAxisJinjinov
either use a Matrix to apply CameraController.Instance.camTransform.eulerAngles to Vector3 moveVector - or - move the player by modifying this.transform.position, without controller.MoveJinjinov

1 Answers

2
votes

this is correct:

I can only assume that the issue is though the forward axis has changed for the object the x,y,z in world space doesnt change and thats what MainVertical() is still accessing

you are confusing this.transform from player and Input.GetAxis - they are 2 totally different things, unrelated to each other. imagine FIFA, where you have 11 soccer players, each has his own this.transform but you still have only one Input.GetAxis

try this:

private void Update(){
    this.transform.eulerAngles = CameraController.Instance.camTransform.eulerAngles;

    //Vector3 moveVector = Quaternion.Euler(this.transform.eulerAngles) * InputManager.MainJoystick ();

    // EDIT
    Vector3 moveVector = InputManager.MainJoystick ();
    moveVector = transform.TransformDirection(moveVector);

    moveVector.x = moveVector.x * walkingSpeed;
    moveVector.y = verticalVelocity;
    moveVector.z = moveVector.z * walkingSpeed;

    controller.Move (moveVector * Time.deltaTime);
}

in general, try to separate your player code from your input code - try to imagine how you would control 11 soccer players with 1 controller

EDIT: used code from https://docs.unity3d.com/ScriptReference/CharacterController.Move.html