H
H
Herman Coffman2021-05-24 17:26:44
Unity
Herman Coffman, 2021-05-24 17:26:44

Charter Controller conflicts with child object, how to fix?

There is a script for turning the camera:

public Transform target;


    void Update()
    {
        float rot_speed = 3f; //коэфицент скорости вращения, чувствительность мыши
        Vector3 delta = new Vector3(-Input.GetAxis("Mouse Y") * rot_speed, Input.GetAxis("Mouse X") * rot_speed, 0f);
        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + delta);
        transform.position = target.position - (transform.forward) + (transform.up);
    }

and a script to control the character through the charter controller:
private float speed;
    public float graviti;
    public float jumpPower;
    public float normalSpeed;
    public float rotateSpeed;

    public Transform head;

    private CharacterController ch_controller;
    private Vector2 input_normalized;
    private Vector3 velocity;
    private float force_down;

    void Start()
    {
        ch_controller = GetComponent<CharacterController>();
    }

    void Update()
    {
        Move();
    }

    private void Move()
    {
        input_normalized.y = Input.GetAxis("Vertical");
        input_normalized.x = Input.GetAxis("Vertical");

        if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
        {
            input_normalized.y = 0;
        }
        if (!Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A))
        {
            input_normalized.x = 0;
        }

        if (Input.GetKeyDown(KeyCode.Space) && ch_controller.isGrounded)
        {
            force_down = jumpPower;
        }
         
        if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
        {
            speed = normalSpeed * 2f;
        }
        else
        {
            speed = normalSpeed;
        }

        if (ch_controller.isGrounded && force_down > 0)
        {
            force_down = jumpPower;
        }
        else
        {
            force_down -= graviti * Time.deltaTime;
        }

        
        velocity = ((transform.forward * input_normalized.y + transform.right * input_normalized.x + head.transform.up * force_down) * speed + Vector3.down * force_down) * Time.deltaTime;
        ch_controller.Move(velocity);
    }

An object with this class is the parent object. And the one with the rotation of the camera is a child.
But it turns out that movement depends on the tilt of the x axis of the camera.
Here is a detailed video with the bug - https://skr.sh/v8DyfGirJwm?a

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question