- Home /
How to make ragdoll arms point towards mouse?
I have made a ragdoll for my game and now I want to make it's arms point towards the mouse. The arms are connected to the torso with hinge joints. Anything would help here is my current code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotate : MonoBehaviour
{
private Vector3 mousePos;
public float addOn;
GameObject point;
void Start() {
point = transform.GetChild(0).gameObject;
}
void Update() {
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
Vector3 difference = mousePos - point.transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
print(rotationZ);
float currentRotation = transform.rotation.z;
transform.RotateAround(point.transform.position,new Vector3(0,0,1),rotationZ);
}
}
Answer by JelyMe · Jul 09, 2021 at 06:22 AM
@BOSS_CODE Hello, this question is a wee bit old now but may I offer some insight. You seem to be modifying the rotation of the object rather than using the hinge joint's rotation. So, if you were to have a poke in these docs: [lhttps://docs.unity3d.com/ScriptReference/HingeJoint2D.html][1] You could modify the jointAngle. :)
Sorry for the limited response I am decently new to Unity but have dealed with this sort of stuff before.
Hope it helped. :) [1]: https://docs.unity3d.com/ScriptReference/HingeJoint2D.html