Search Unity

Manipulating Target and Hinge Joints in C#

Discussion in '2D' started by hausrath, Jan 6, 2018.

  1. hausrath

    hausrath

    Joined:
    Sep 30, 2016
    Posts:
    5
    Hello Community,

    I'm working on some mechanics that are similar in some ways to the controls in "Mount Your Friends." In C#, I am attempting to hold the "E" key to select the player's right forearm, then OnMouseDown, I'd like to move that forearm toward the mouse position. Code is below. Everything compiles, but the target joint just plain doesn't move. If you have advice or would rather point me towards a tutorial, I'd greatly appreciate. Please let me know if you need additional information.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class scr_forearm_r : MonoBehaviour {
    6.  
    7.     private bool isPressed = false;
    8.  
    9.     private void Update()
    10.     {
    11.         if (Input.GetKeyDown(KeyCode.E))
    12.         {
    13.             GetComponent<Renderer>().material.color = Color.red;
    14.         }
    15.  
    16.         if (Input.GetKeyUp(KeyCode.E))
    17.         {
    18.             GetComponent<Renderer>().material.color = GetComponent<SpriteRenderer>().color;
    19.         }
    20.  
    21.     }
    22.  
    23.     void OnMouseDown()
    24.     {
    25.         isPressed = true;
    26.         GetComponent<TargetJoint2D>().target = (Camera.main.ScreenToWorldPoint(Input.mousePosition));
    27.     }
    28.  
    29.     private void OnMouseUp()
    30.     {
    31.         isPressed = false;
    32.      
    33.     }
    34. }