Can't move rigidbody with player

So I’ve made my own player controller with the Character Controller component, also I’m using the Rigidbody as well on it to simulate gravity and be able to interact with other rigidbodys. But, when I try to move my crate by walking in to it with the player it wont budge.

Player Movement Script:
```csharp
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

public float movementSpeed = 2f;
public float jumpForce = 15f;

float moveFB;
float moveLR;
float verticalVelocity;

CharacterController player;
Rigidbody rgbd;

void Start()
{
    player = GetComponent<CharacterController>();
    rgbd = GetComponent<Rigidbody>();
}

void Update()
{
    if(Input.GetButtonDown("Jump"))
        Jump();

    moveFB = Input.GetAxis("Vertical") * movementSpeed;
    moveLR = Input.GetAxis("Horizontal") * movementSpeed;

    Vector3 movement = new Vector3(moveLR * Time.deltaTime, rgbd.velocity.y, moveFB * Time.deltaTime);

    movement = transform.rotation * movement;
    player.Move(movement);
}

void FixedUpdate()
{
    verticalVelocity += Physics.gravity.y * 0.05f;
}

void Jump()
{
    verticalVelocity = 0f;
    verticalVelocity += jumpForce;
}

}**
```

Components attached to the player:

  • Character Controller
  • Rigidbody
  • PlayerController
  • MouseLook

Components attached to the crate:

  • Cube mesh
  • Box Collider
  • Mesh Renderer
  • Rigidbody

What am I doing wrong?

As far as I’m aware the Move function wont apply a force to colliders, it just constraints movement depending on collisions.

By the way, you don’t need to set the velocity to 0 and then add jumpForce, you can just set it to jump force (0 + jumpForce will always be just jumpForce). It also grows unbounded in the FixedUpdate call, there’s no check for whether or not it’s on the ground.

2 Likes

Okey, I guess I’ll have to make a Rigidbody based controller then if I want the player to affect other rigidbodys then. Thank you.

Alternatively you can use the OnCollisionEnter call or something to get a list of ContactPoints, then from those you could add a force in the direction your player is trying to move, if at all.

A purely physics based controller is usually a better idea, except in edge cases like VR, where you usually want no acceleration, with movement in the render loop instead of the physics loop (jerky camera…).

1 Like