Question by
nikitabaldan · Mar 11, 2020 at 01:51 PM ·
c#rotationscripting problemgameobjectmouse-drag
Rotate GameObject with children around itself on mouse drag
Hi, I'd like to rotate my GameObject with a set of spheres as children around itself. Until now I've written the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectRotator : MonoBehaviour{
Vector3 mPrevPos = Vector3.zero;
Vector3 mPosDelta = Vector3.zero;
void Update(){
if(Input.GetMouseButton(0)){
mPosDelta = Input.mousePosition - mPrevPos;
if(Vector3.Dot(transform.up, Vector3.up)>=0){
transform.Rotate(transform.up, -Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.Self);
}
else{
transform.Rotate(transform.up, Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.Self);
}
transform.Rotate(Camera.main.transform.right, Vector3.Dot(mPosDelta, Camera.main.transform.up), Space.Self);
}
mPrevPos = Input.mousePosition;
}
}
But this code does not work as I want to. I'd like that my GameObject stays in the same position, but on mouse drag it rotates. Could you help me please?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612224748im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help! Script won't work. 1 Answer
Why my code to rotate object doesn't work in normal runtime, but does in debugging? 0 Answers
After Rotating a Prefab, Transform.Position of children is inaccurate 1 Answer
How to apply script to a imported OBJ file in unity? 0 Answers
RPG instantiate problem 3 Answers