- Home /
Why the npc character walking strange when using a Rigidbody and Is Kinematic on enabled ?
The details:
The character have been downloaded from Mixamo.com
The main goal to make the character walk through some doors each door to trigger the door to open/close when entering and exiting the box collider area.
This is not the player and not a first person or third person controller. It's npc not enemy but just a character that when running the game he start walking automatic using Animator and the HumanoidWalk animation.
I had to make some changes on the character animation: In the Animation tab in the Inspector I checked enabled: Root Transform Rotation Bake Into Pose and also Root Transform Position (Y) Bake Into Pose and both set Based Upon to original.
The last one Root Transform Position (XZ) Bake Into Position is unchecked disabled and Based Upon set to original.
On the character in the inspector I have a Rigidbody and a box collider.
The Rigidbody settings:
Mass = 1 Drag = 0 Angular Drag = 0.05
Use Gravity is unchecked disabled
Is Kinematic checked enabled.
If I'm not using Is Kinematic the character will float in the air and will rotate strange and move randomly. Only using Is Kinematic make the player moving and walking forward and triggering the doors.
The problem:
The problem is that since Is Kinematic is enabled when not triggering doors just when walking the character seems to be shaking/stuttering a bit. It's not walking smooth if there was no Rigidbody.
But without a Rigidbody the doors will never trigger.
What I did before was using a script on the character name Collision and inside OnTriggerExit I destroyed the Rigidbody.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collision : MonoBehaviour
{
public GameObject door;
public Animator character;
public DoorsLockManager doorslockmanager;
private float speed = 0;
private bool triggered = false;
private void OnTriggerEnter(Collider other)
{
if (other.name == door.name &&
doorslockmanager.locked == true)
{
triggered = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.name == door.name &&
doorslockmanager.locked == false)
{
var rigid = character.GetComponent<Rigidbody>();
Destroy(rigid);
}
}
// Update is called once per frame
void Update()
{
float distanceFromTarget = Vector3.Distance(character.transform.position, door.transform.position);
if (triggered == true)
{
speed = (distanceFromTarget / 5);
character.SetFloat("Walking Speed", speed);
character.SetBool("Idle", true);
}
}
}
The problem now is that after destroying the Rigidbody when he enter the next second door trigger area it won't trigger the door since there is no Rigidbody anymore.
I can't solve this shaking/stuttering when using Is Kinematic
Answer by rohitkvasanth · Feb 06, 2019 at 07:04 AM
@DubiDuboni I tried this just now, but with my own code and some basic shapes. it worked fine. I made a sphere as a player with RB attached and isKinematic set to false; placed a collider around the door with trigger enabled ( not the door, but another cube with mesh rendererer disabled.) I only made isKinematic false since i was using velocity to move it, but it works with kinematic RBs as well if transform is used. If you really want the kinematic property to be enabled and disabled as needed, you can access it directly from the script. ie. GetComponent().isKinematic = true/false; instead of destroying the gameobject.
I tried now with another character not from $$anonymous$$ixamo.
When I disable the Is $$anonymous$$inematic on the character he get crazy moving up down rotating shaking. Only when Is $$anonymous$$inematic is enabled checked he walk forward using the HumanoidWalk animation. But then also a bit shaking/stuttering.
Then I tried to disable the Animator so no animation will be involved. And added a small script to the character to move him forward:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveforward : $$anonymous$$onoBehaviour
{
public float movementSpeed = 1;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position += transform.forward * Time.deltaTime * movementSpeed;
}
}
Now when running the game the character fall down and get crazy. I tried to disable also the box collider on the character and then he just fall down straight.
The Rigidbody Is $$anonymous$$inematic set to false disabled but Use Gravity is enabled. But if Use Gravity is disabled the character fall either.
I can't figure it out.
can you just comment this line and tell me if its still stuttering ? character.SetFloat("Walking Speed", speed);
have no clue whats wrong with the rigidbody, could you add a screenshot of the inspector for the player RB ? try using rigidbody position constraints in Y and rotation constraints in X and Z if all else fails.
Your answer
Follow this Question
Related Questions
Check for collision while animating 0 Answers
How to enter trigger a certain amount of times before executing code? 1 Answer
Trigger help! 0 Answers
Ideas for detecting collision speed with trigger? 2 Answers
Check if trigger is occupied 1 Answer