Skip to content

Commit

Permalink
Swaying animation during moving
Browse files Browse the repository at this point in the history
  • Loading branch information
JI0PATA committed Sep 27, 2023
1 parent fbe4dcc commit 3e986e8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
4 changes: 0 additions & 4 deletions src/App.jsx
Expand Up @@ -28,10 +28,6 @@ export const App = () => {
<Player />
<Cubes />
</Physics>

<group position={[3, 1, -2]}>
<WeaponModel />
</group>
</>
)
}
Expand Down
55 changes: 53 additions & 2 deletions src/Player.jsx
@@ -1,7 +1,8 @@
import * as THREE from "three";
import * as RAPIER from "@dimforge/rapier3d-compat"
import * as TWEEN from "@tweenjs/tween.js";
import {CapsuleCollider, RigidBody, useRapier} from "@react-three/rapier";
import {useRef} from "react";
import {useEffect, useRef, useState} from "react";
import {usePersonControls} from "./hooks.js";
import {useFrame} from "@react-three/fiber";
import {Weapon} from "./Weapon.jsx";
Expand All @@ -17,6 +18,11 @@ export const Player = () => {
const { forward, backward, left, right, jump } = usePersonControls();
const objectInHandRef = useRef();

const swayingObjectRef = useRef();
const [swayingAnimation, setSwayingAnimation] = useState(null);
const [swayingBackAnimation, setSwayingBackAnimation] = useState(null);
const [isSwayingAnimationFinished, setIsSwayingAnimationFinished] = useState(true);

const rapier = useRapier();

useFrame((state) => {
Expand Down Expand Up @@ -46,12 +52,55 @@ export const Player = () => {
// moving object in hand for the player
objectInHandRef.current.rotation.copy(state.camera.rotation);
objectInHandRef.current.position.copy(state.camera.position).add(state.camera.getWorldDirection(rotation));

const isMoving = direction.length() > 0;

if (isMoving && isSwayingAnimationFinished) {
setIsSwayingAnimationFinished(false);
swayingAnimation.start();
}

TWEEN.update();
});

const doJump = () => {
playerRef.current.setLinvel({x: 0, y: 8, z: 0});
}

const initSwayingObjectAnimation = () => {
const currentPosition = new THREE.Vector3(0, 0, 0);
const initialPosition = new THREE.Vector3(0, 0, 0);
const newPosition = new THREE.Vector3(-0.05, 0, 0);
const animationDuration = 300;
const easing = TWEEN.Easing.Quadratic.Out;

const twSwayingAnimation = new TWEEN.Tween(currentPosition)
.to(newPosition, animationDuration)
.easing(easing)
.onUpdate(() => {
swayingObjectRef.current.position.copy(currentPosition);
});

const twSwayingBackAnimation = new TWEEN.Tween(currentPosition)
.to(initialPosition, animationDuration)
.easing(easing)
.onUpdate(() => {
swayingObjectRef.current.position.copy(currentPosition);
})
.onComplete(() => {
setIsSwayingAnimationFinished(true);
});

twSwayingAnimation.chain(twSwayingBackAnimation);

setSwayingAnimation(twSwayingAnimation);
setSwayingBackAnimation(twSwayingBackAnimation);
}

useEffect(() => {
initSwayingObjectAnimation();
}, []);

return (
<>
<RigidBody colliders={false} mass={1} ref={playerRef} lockRotations>
Expand All @@ -61,7 +110,9 @@ export const Player = () => {
</mesh>
</RigidBody>
<group ref={objectInHandRef}>
<Weapon position={[0.3, -0.1, 0.3]} scale={0.3} />
<group ref={swayingObjectRef}>
<Weapon position={[0.3, -0.1, 0.3]} scale={0.3} />
</group>
</group>
</>
);
Expand Down

0 comments on commit 3e986e8

Please sign in to comment.