K
K
kokapuk2021-04-14 12:30:45
Unity
kokapuk, 2021-04-14 12:30:45

Character can't push objects?

Hello, on the stage I have a capsule with a Character Controller, a floor and a sphere with a Rigidbody, I also have a script that moves the character to wasd

using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class Player : MonoBehaviour
{
    public float speed = 4f;
    public float jumpForce = 8f;
    public float gravity = 20f;
    private Vector3 moveDir = Vector3.zero;
    private CharacterController controller;

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

    private void FixedUpdate()
    {
        if (controller.isGrounded)
        {
            moveDir = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
            moveDir = transform.TransformDirection(moveDir);
            if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Vertical") != -1)
            {
                moveDir *= 2 * speed;
            }
            else
            {
                moveDir *= speed;
            }
            
        }

        if (Input.GetKeyDown(KeyCode.Space) && controller.isGrounded)
        {
            moveDir.y = jumpForce;
        }
        moveDir.y -= gravity * Time.deltaTime;
        controller.Move(moveDir * Time.deltaTime);
    }
}

so, my problem is that the character can't push the sphere to make it move, it works with other objects, but on them I have to throw a Rigidbody and move them with velocity, then yes, they can push the sphere. Is there a way to do this through the Character Controller?

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