Following tutorial will guide you how to rotate an object using rigidbody.I assume you might be aware of basics about unity.There are different ways for rotating object’s, For example :: rotation using rigidbody, rotation using Quaternion etc .
- Choosing the technique for rotating an object depends on the “type of object”, By “Type of object” i mean weather the object has a rigidbody component, character controller or nothing attached it it.
- Following technique of rotation is best suited only for those object which have rigidbody component attached to it.
- If your object is not using any component for Movement and Rotation then refer following link
How to rotate object in unity using Quaternion - Create a sample project and add a plane to the scene along with a cube(TargetObject) towards which we have rotate our rotationObject( player just to differentiate between two objects).
- Now check “isTrigger” option of rotation object(player) and add rigidbody component to it. You can also lock rotation along any axis in “Inspector”.
using UnityEngine; using System.Collections; public class RigidbodyRotation : MonoBehaviour { float rotationSpeed=0.5f ;// This Must be less than 1 and greater than 0 GameObject targetObject=null; // Use this for initialization void Start () { // get target object to be rotated at targetObject=GameObject.FindGameObjectWithTag(“TargetObject”) as GameObject; } // Update is called once per frame void Update () { } void FixedUpdate () { if(targetObject==null){ Debug.Log(“[ERROR] => Traget object is null , Can’t Rotate”); return; } // get position of target object Vector3 targetPosition=targetObject.transform.position; // gives us vector to direction of target Vector3 inverseVect=transform.InverseTransformPoint(targetPosition); // calculate angle by which you have to rotate // Note -: This angle is calculated every Frame of FixedUpdate float rotationAngle=Mathf.Atan2(inverseVect.x,inverseVect.z)*Mathf.Rad2Deg; // Now calculate rotationVelocity to be applied every frame Vector3 rotationVelocity=(Vector3.up*rotationAngle)*rotationSpeed*Time.deltaTime; // Calaculate his delta velocity i.e required – current Vector3 deltavel=(rotationVelocity-rigidbody.angularVelocity); // Apply the force to rotate rigidbody.AddTorque(deltavel,ForceMode.Impulse); } }
- Now run the scene , And you can see Player object rotating to target object.
- Note :: Also look for different types of forces to get variations.
Disclaimer
All content provided on this blog is for informational purposes only. Netfotech Solutions provides no endorsement and makes no representations as to accuracy, reliability, completeness, suitability or validity of any information or content on, distributed through or linked, downloaded, or accessed from this site.
Netfotech Solutions will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use of the information on this site. All information is provided on an as-is basis without any obligation to make improvements or to correct errors or omissions. This site may contain links to other web sites. Netfotech Solutions makes no guarantees or promises regarding these web sites and does not necessarily endorse or approve of their content. You may not modify any part of the blog. Inclusion of any part of this blog in another work, whether in printed or electronic or other form, or inclusion of any part of the blog in another web site by linking, framing or otherwise without the express permission of Netfotech Solutions is prohibited. This site may not be used for any illegal or illicit purpose and Netfotech Solutions reserves the right, at its sole discretion and without notice of any kind, to remove anything posted to this site. By using this site, you hereby acknowledge that any reliance upon any materials shall be at your sole risk.