- Home /
Rotating rigidbodies with moveRotation along external axis
Hello, I'm in a process of programming a robotic arm in Unity. I've created 3D model and would want to move them but with a caveat that if (for example) I rotate the base, the arm rotates as well. The catch? I wanted Unity to handle collisions for base objects (like cubes, spheres, etc.). My goal is to have objects which I could pick up and let go. At first I've tried using " transform.rotate" in order to move the arm's joints. It all seemed great, as I had parented the objects from base - up and whenever I " transform.rotate" the base, the rest of the robotic arm rotates as well. But then I was told that one should use rigidbodies for collisions and this is where things have started to fall apart. First thing is that when I use rigidbodies, they no longer rotate when parented (when using “transform.rotate”). Second, when I use “transform.rotate” I'm able to phase into objects. I guess that’s since this function works as a teleport of sort. And so, I wanted to use “Rigidbody.MoveRotation”. This works, but only for one of the elements. They, of course, don't move when parented, so I've thought that maybe I could use “Rigidbody.MoveRotation” to rotate each of the arm's elements along their "lower tier" element's axis (see tiers below)
base – boom – arm - gripper
but I'm stuck at getting it to work. Am I understanding correctly, that “Rigidbody.MoveRotation” applies rotation ONLY along the local rotation axis?
I paste my code below, but I don't think if will be of any use:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Manipulator_movement : MonoBehaviour
{
public GameObject Obrotnica;
public GameObject Wypad;
public GameObject Ramie;
public float myInput;
public Vector3 m_EulerAngleVelocity;
public float tempRotationOfObrotnica;
public Quaternion tempRotationOfObrotnicaQ;
public Quaternion GG;
// Start is called before the first frame update
void Start()
{
GG = Wypad.transform.rotation;
}
void Update()
{
if (Input.GetButton("Right_Shoulder_Trigger") || Input.GetButton("Left_Shoulder_Trigger"))
{
Debug.Log("LJH: " + Input.GetAxis("Left_Joy_Horizontal") + "LJV: " + Input.GetAxis("Left_Joy_Vertical") + "RJH: " + Input.GetAxis("Right_Joy_Horizontal") + "RJV: " + Input.GetAxis("Right_Joy_Vertical"));
// this is for controller support debugging
}
else
{
//obrotnicaAngleSpeed = 0;
}
}
void FixedUpdate()
{
Rigidbody temp_rigidB_Obrotnica = Obrotnica.GetComponent<Rigidbody>();
Rigidbody temp_rigidB_Wypad = Wypad.GetComponent<Rigidbody>();
Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity * myInput * Time.deltaTime);
temp_rigidB_Obrotnica.MoveRotation(temp_rigidB_Obrotnica.rotation * deltaRotation);
tempRotationOfObrotnica = temp_rigidB_Obrotnica.transform.rotation.eulerAngles.y;
//tempRotationOfObrotnicaQ = Obrotnica.transform.rotation;
tempRotationOfObrotnicaQ = Quaternion.AngleAxis(Obrotnica.transform.rotation.y,new Vector3(0,Obrotnica.transform.rotation.y,0));
//temp_rigidB_Wypad.MoveRotation(Obrotnica.transform.rotation);
temp_rigidB_Wypad.MoveRotation(GG);
//temp_rigidB_Wypad.MoveRotation(temp_rigidB_Wypad.rotation * tempRotationOfObrotnicaQ);
//Obrotnica.transform.Rotate(0f,obrotnicaAngleSpeed*Time.deltaTime,0f,Space.Self);
//Obrotnica.transform.ro
}
}
Maybe there’s a different way to do it and I'm just to blind to see it?
I'd be grateful for any help you guys can spare. Thanks in advance.
Cheers, Rafał
Your answer
![](https://koobas.hobune.stream/wayback/20220613014637im_/https://answers.unity.com/themes/thub/images/avi.jpg)