Search Unity

Unity 5 - hinge joint doenst work

Discussion in 'Scripting' started by Aiursrage2k, Mar 5, 2015.

  1. Aiursrage2k

    Aiursrage2k

    Joined:
    Nov 1, 2009
    Posts:
    4,835
    It seems like when I apply a hinge joint targetposition that it doesnt change -- only if change the inspector will it work. Its changing the inspector value but doesnt actually do anything to the joint, only if i manually enter the data in the inspector will it work.


    JointSpringsp = hinge.spring;
    sp.spring = hinge.spring.spring;
    sp.targetPosition = tp;
    hinge.spring = sp;

     
    Last edited: Mar 5, 2015
  2. Aiursrage2k

    Aiursrage2k

    Joined:
    Nov 1, 2009
    Posts:
    4,835
  3. Aiursrage2k

    Aiursrage2k

    Joined:
    Nov 1, 2009
    Posts:
    4,835
    Hey guys I came up with a solution that works okay I guess at least for my needs.

    Code (CSharp):
    1.  
    2.        //therigidbody
    3. private Rigidbodym_rigidBody;
    4.  
    5. //theinitalrotation
    6. private Quaternionm_intialRot;
    7.  
    8. //theinitalup
    9. private Vector3 m_initalUp;
    10. public void Awake()
    11. {
    12. m_rigidBody = gameObject.GetComponent<Rigidbody>();
    13. m_intialRot = transform.rotation;
    14. m_initalUp = transform.up;
    15. }
    16.  
    17.   //we hit a ball apply a force
    18.         public void OnCollisionEnter(Collision col)
    19.         {
    20.             if(col.gameObject.name.Contains("Player"))
    21.             {
    22.                 ContactPoint cp = col.contacts[0];
    23.                 Vector3 newPos = Vector3.Reflect(col.transform.position, cp.normal);
    24.  
    25.                 Vector3 dir = newPos - col.transform.position;
    26.                 col.rigidbody.AddForce( dir.normalized * hitPower * m_power ,ForceMode.Impulse);
    27.             }
    28.         }
    29.         public void handleRotateAndPower()
    30.         {
    31.             if(m_rotate)
    32.             {
    33.                 m_speed += Time.deltaTime * rotSpeed;
    34.                 m_speed = Mathf.Clamp(m_speed,minSpeed,maxSpeed);
    35.            
    36.                 m_power = m_speed / maxSpeed;
    37.                 if(m_power<0)
    38.                 {
    39.                     m_power *= -1;
    40.                 }
    41.            
    42.                 m_angle0 += Time.deltaTime * m_speed;
    43.                 m_angle0 = Mathf.Clamp(m_angle0,minAngle,maxAngle);
    44.            
    45.             }else{
    46.                 m_speed -= Time.deltaTime * rotSpeed;
    47.                 m_speed = Mathf.Clamp(m_speed,minSpeed,maxSpeed);
    48.                 float val = m_speed / maxSpeed;
    49.                 m_power = 1.0f + (m_speed / maxSpeed);
    50.                 if(val > 0)
    51.                 {
    52.                     m_power = 1.0f - (m_speed / maxSpeed);
    53.                 }
    54.                 if(m_power<0)
    55.                 {
    56.                     m_power *= -1;
    57.                 }
    58.            
    59.                 m_angle0 += Time.deltaTime * m_speed;
    60.                 m_angle0 = Mathf.Clamp(m_angle0,minAngle,maxAngle);
    61.             }
    62.        
    63.  
    64.         }
    65.        void LateUpdate () {
    66.  
    67. handleRotateAndPower();
    68.  
    69. transform.rotation = Quaternion.AngleAxis(m_angle0, m_initalUp);
    70. }
    71.