Search Unity

RigidBody.MovePosition

Discussion in 'Physics' started by Ruchir, Jun 29, 2019.

  1. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    I'm having problem with the function RigidBody.Moveposition , it just doesn't move if the rigidbody is to kinematic , I checked the documentation but didn't find anything there either , am I missing something??
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    What's the code?
    Did you attached the script to the GO?

    upload_2019-6-30_3-24-14.jpeg
     
  3. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    of course I did attach the script to the game object
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TheCOLDDev.Forces;
    5.  
    6. namespace TheCOLDDev
    7. {
    8.     public class TM_CharacterController : MonoBehaviour
    9.     {
    10.         [Header("Reference")]
    11.         public Rigidbody m_Body;
    12.         public CapsuleCollider m_Collider;
    13.         public CapsuleCollider m_CollidsionChecker;
    14.  
    15.         [Header("Values")]
    16.         public Vector3 m_Velocity = Vector3.zero;
    17.         public float OverlapCheckRadius = 1;
    18.         public LayerMask OverlapLayer;
    19.  
    20.         [Header("Gravity")]
    21.         public bool m_Grounded;
    22.         public List<TC_Gravity> m_GravityForces = new List<TC_Gravity>();
    23.         public LayerMask GroundMask;
    24.         public Vector3 m_GravityForcesVelocity = Vector3.zero;
    25.         public Vector3 m_GravityVelocity;
    26.  
    27.         protected Vector3 p_FinalVelocity = Vector3.zero;
    28.    
    29.  
    30.  
    31.         // Start is called before the first frame update
    32.         void Start()
    33.         {
    34.            
    35.         }
    36.    
    37.         // Update is called once per frame
    38.         void FixedUpdate()
    39.         {
    40.             m_GravityForcesVelocity = Vector3.zero;
    41.             p_FinalVelocity = Vector3.zero;
    42.             f_CalculateVelocity();
    43.             f_CalculateGravity();
    44.             f_MoveCharacter();
    45.             Depentrate();
    46.            
    47.         }
    48.         #region Movement
    49.         protected virtual void f_CalculateVelocity()
    50.         {
    51.             p_FinalVelocity += m_Velocity;
    52.         }
    53.  
    54.         protected virtual void f_MoveCharacter()
    55.         {
    56.             m_Body.MovePosition(m_Body.position + p_FinalVelocity * Time.fixedDeltaTime);
    57.         }
    58.         protected virtual void f_CalculateGrounded()
    59.         {
    60.             RaycastHit hit;
    61.            m_Grounded = Physics.SphereCast(m_Collider.transform.position + m_Collider.center , m_Collider.radius  , m_GravityForcesVelocity  , out hit , m_Collider.height / 2 , GroundMask ,QueryTriggerInteraction.UseGlobal);
    62.         }
    63.         protected virtual void f_CalculateGravity()
    64.         {
    65.             for(int i =0; i < m_GravityForces.Count;i++)
    66.             {
    67.                 m_GravityForcesVelocity += m_GravityForces[i].GetGravity(m_Body.position);
    68.             }
    69.             f_CalculateGrounded();
    70.             m_GravityVelocity += m_GravityForcesVelocity * Time.fixedDeltaTime;
    71.             if(m_Grounded)
    72.                 m_GravityVelocity = Vector3.zero;
    73.             m_Velocity += m_GravityVelocity;
    74.         }
    75.         #endregion
    76.    
    77.         #region Depentration
    78.         public virtual void Depentrate()
    79.         {
    80.             Collider[] p_Colliders = new Collider[4];
    81.             Vector3 DepentrationVector = Vector3.zero;
    82.  
    83.             int i_ = Physics.OverlapSphereNonAlloc(transform.position , OverlapCheckRadius , p_Colliders ,OverlapLayer , QueryTriggerInteraction.UseGlobal);
    84.             for( int i =0; i < i_;i++)
    85.             {
    86.                  Vector3 DepentrationDirection = Vector3.zero;
    87.                  float DepentrationDistance = 0;
    88.                 Physics.ComputePenetration(m_Collider , transform.position + m_Collider.center , transform.rotation, p_Colliders[i] , p_Colliders[i].transform.position , p_Colliders[i].transform.rotation,out DepentrationDirection , out DepentrationDistance);
    89.                 DepentrationVector += DepentrationDirection*DepentrationDistance;
    90.              //   Debug.Log(i_);
    91.              //   Debug.Log(p_Colliders[i].gameObject);
    92.             }
    93.             m_Body.MovePosition(m_Body.position + DepentrationVector);
    94.         }
    95.         #endregion
    96.     }
    97.  
    98. }
    99.  
    Ignore the gravity part it just returns a vector3 in required direction.
    even if it was buggy I tried same thing thing without it and setting the isKinematic to true in the inspector and the rigidbody stopped moving suddenly , I don't know what's causing this at all:confused::mad::mad:
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Start slapping Debug.Logs in there to see what it's doing, make sure that p_FinalVelocity actually has the value you think it has.
     
  5. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    yes it has that velocity it moves fine when not kinematic but as soon as I make it kinematic it just stop,I'm really getting frustrated over this, I mean MovePosition are mainly for kinematic bodies then what the heck is wrong with this???:mad::mad:
     
  6. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    I found out something interesting , I'm able to call MovePosition only once per fixed update from my script,is this officially documented somewhere???o_O