Friday, February 28, 2025
spot_img

Tips on how to correctly use CharacterController in a 3d Physics world? – Cocos Creator


Hey, I’m studying learn how to correctly set a CharacterController script for a TPS character along with native physics, however I’m not having a lot success.

If I simply use the CharaterController + the Inflexible physique, collisions don’t work.
ezgif-2-0e35975fe4

And if I add a cylinder collider, it doesn’t get up.
ezgif-2-c0debfa6ba

What can be one of the best ways to make use of TPS CharacterController + actual physics simulation world?

hello, please set angular forces to 0
and you’ll refer this demo(controller.zip – Google Drive)



1 Like

Thanks @iwae!

Along with your challenge instance, I used to be capable of perceive higher and pull it off!

Right here is my code for reference if anybody is attempting to do it.

import {
    _decorator,
    animation,
    CapsuleCharacterController,
    CharacterControllerContact,
    clamp,
    Element,
    lerp,
    PhysicsSystem,
    Vec3,
} from 'cc';

import OrbitCamera from '../digital camera/orbitCamera';
import inputMap from '../motion/enter';
const { ccclass, property, sort } = _decorator;

@ccclass('Character')
export class Character extends Element {
    @property
    walkSpeed!: quantity;

    @property
    runSpeed!: quantity;

    @property
    jumpForce!: quantity;

    @property
    gravityValue!: quantity;

    @property
    linearDamping!: quantity;

    @property
    pushPower!: quantity;

    @sort(OrbitCamera)
    digital camera!: OrbitCamera;

    @sort(animation.AnimationController)
    animationCtrl!: animation.AnimationController;

    @sort(CapsuleCharacterController)
    characterCtrl!: CapsuleCharacterController;

    non-public controlZ = 0;
    non-public controlX = 0;

    non-public motion = new Vec3(0, 0, 0);
    non-public tempVec3 = new Vec3(0, 0, 0);
    non-public playerVelocity = new Vec3(0, 0, 0);

    non-public currentSpeed = 0;
    non-public tempRotation = 0;
    non-public targetRotation = 0;

    protected onLoad(): void {
        this.characterCtrl.on('onControllerColliderHit', this.onControllerColliderHit, this);
    }

    onControllerColliderHit(hit: CharacterControllerContact): void {
        const physique = hit.collider.attachedRigidBody;
        if (physique == null || physique.isKinematic) {
            return;
        }

        // We dont need to push objects beneath us
        if (hit.motionDirection.y 



1 Like

Your third particular person view may be very easy, how did you do it?

Please are you able to present the character controller code, with out animation?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles