- Home /
transform.RotateAround problems
I'm trying to rotate a door gameObject using the transform.RotateAround method and I'm getting some weird behavior. Now, before anyone asks, I'm doing it this way instead of using an animation for a reason, so please don't tell me to use an animation instead. Instead of behaving as a door should, the door comes off of the point where its hinge should be and rotates further off into the distance with each mouse click. Here's a snippet:
AngleToOpen is the angle I want to rotate the object to
transform.RotateAround(this.hinge, Vector3.up, -this.AngleToOpen);
Thanks for any help that anyone may be able to give.
Answer by swhite · Sep 03, 2010 at 08:27 PM
Try rotating a smaller object around the hinge (think of it as the door hinge part) then child your door to this smaller object so that it moves in rotation with the hinge.
Example:
Image Link : (quick and cheesy door example picture ;) ) Basically here's what you 'could' do.
You have your door frame a hinge on the frame, a hinge on the door and the door.
You then rotate the hinge on the door around the hinge on the door frame.
transform.RotateAround (hinge.transform.position, Vector3.up, 20 * Time.deltaTime);
Then you add any constraints that you want (IE don't rotate past this point, stop on collision, whatever you want)
Answer by bubbalovesponge · Nov 13, 2013 at 07:53 PM
/////////////////////////////////////////////////////////////////////////////////
//
// ThisOpenDoor.cs
// Created: Darren Hedlund microcyb@gmail.com
//
// description: Script to open a door when the Player is near
//
// Note: To change the pivot point use this URL by VoxelBoy.
// http://solvethesystem.wordpress.com/2010/01/15/solving-the-pivot-problem-in-unity/
// or create a seperate Empty GameObject and attach door mesh to it
/////////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using System.Collections;
public class ThisOpenDoor : MonoBehaviour
{
float smooth = 2.0f;
float DoorOpenAngle=90.0f;
float DoorCloseAngle=0.0f;
Quaternion target;
private Transform _target;
void Start()
{
DoorCloseAngle=this.transform.localEulerAngles.y;
if(DoorCloseAngle<=0)
{
DoorOpenAngle=90.0f;
}
else
{
DoorOpenAngle=this.transform.localEulerAngles.y-90.0f;
}
}
void Update()
{
GameObject _target = GameObject.FindGameObjectWithTag("Player");
if (_target != null)
{
float dist = Vector3.Distance(_target.transform.position, this.transform.position);
if (dist < 8.00f)
{
target= Quaternion.Euler (0, DoorOpenAngle, 0);
this.transform.localRotation = Quaternion.Slerp(this.transform.localRotation, target, Time.deltaTime * smooth);
}
if (dist > 8.00f)
{
target= Quaternion.Euler (0, DoorCloseAngle, 0);
this.transform.localRotation = Quaternion.Slerp(this.transform.localRotation, target, Time.deltaTime * smooth);
}
}
}
}
Your answer
Follow this Question
Related Questions
Open door with key 2 Answers
Rotate object around object with fixed value. 1 Answer
Rotate RigidBody 2D with respect to velocity 0 Answers
RotateAround Limitations 2 Answers
Door opening in the wrong direction..... 0 Answers