- Home /
Question by
Seppi · Oct 18, 2019 at 03:46 PM ·
scripting problemphisics
Trying to manually limit the velocity of a rigidbody.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Corpse : MonoBehaviour{
Rigidbody rb;
void Start(){
rb = gameObject.transform.GetChild(1).GetChild(1).gameObject.GetComponent<Rigidbody>();
}
void FixedUpdate(){
if(rb.velocity.y > 10){
rb.velocity = new Vector3(rb.velocity.x, 10, rb.velocity.z);
}
if(rb.velocity.z > 3){
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, 3);
}
Debug.Log(rb.velocity);
}
}
"rb" is the rigidbody component on the pelvis of a ragdoll. From the Debug.Log I can tell that it isn't working at all. What am I missing?
Comment
ıt worked fine when ı deleted "GetChild(1).GetChild(1)" part.
Well, the object I'm trying to access is the second child of the second child of the object this script is on...
It might be a better design to have a public RigidBody variable in which you drop the child.
Also, your code could be simplified to:
rb.velocity = Vector3.ClampVelocity(rb.velocity, 10f);
To limit the velocity at 10.