I’m trying to make an object rotate when the player collides with it. I’m attaching this script to the object that the player will collide with. Both have rigid bodies as expected.
I’m trying to make the script so that when something collides with the object, it will rotate (for a specifiable time and speed). I want the rotations to continue after the collision.
Can anyone guide me to the correct route to take on this one?
I have placed a simple “space bar” key press so as to test the variable counting down and that aspect works, however the collision with the object makes no difference to outcome.
I would also like to have more control over the time it takes and degrees of rotation completed.
Below is my failed attempt 
using UnityEngine;
using System.Collections;
public class TouchRotationScript : MonoBehaviour {
public float YRotationSpeed =4.0f;
public float timeAdjustment = 40f;
private float RotationSeconds = 0.0f;
private Transform MyTransform;
void Start()
{
MyTransform = transform;
}
void Update ()
{
if(RotationSeconds>1.0f)
{
MyTransform.Rotate(0.0f, YRotationSpeed * Time.deltaTime * timeAdjustment, 0.0f);
RotationSeconds -= 1.0f;
}
if(Input.GetButton("Jump")){
RotationSeconds = 100.0f;
}
}
void OnCollisionEnter()
{
RotationSeconds = 100.0f;
}
}
edit: see latest post
Hope this javascript helps.
Rotates objekt on collision for a specified amount of time at a specified speed.
I'm kinda new at scripting, but this is how I would do it.
I put the rotating object to is kinematik.
var rotationSpeed : int = 100;
var activateRotation : boolean = false;
var rotationAmount : int = 40;
//sets boolean to false on startup
function Start()
{
activateRotation = false;
}
//set the boolean to true, when collision occurs
function OnCollisionEnter()
{
activateRotation = true;
}
//function update makes sure the rotation keeps on going
function Update()
{
if(activateRotation == true)
{
//rotates the object around the y-axis in a specified speed
transform.Rotate(Vector3.up*Time.deltaTime*rotationSpeed);
//if the angle of the objekt on the y-axis reaches a degree stop the rotation
if(transform.eulerAngles.y > rotationAmount)
{
activateRotation = false;
}
}
}
Thanks very much for the reply, I’ve managed to find a work around using an “OnTriggerEnter” instead of a collision which seems to work better for starting it.
The section on the rotation angle is very useful and was exactly what I was looking for.
Thanks, problem solved. 
New problem. I am trying to detect when an object has completed a full 360 turn by detecting the “0” degree each time it passes.
Unfortunately it does not always get detected as it may be rotating out of reach or not by exact values.
This ends up with the object rotating indefinitely because it can’t pick up on the “<= 0.0f” value.
// Rotate the main object
MyTransform.Rotate(0.0f, TurnSpeed, 0.0f);
if(MyTransform.eulerAngles.y <= 0.0f)
{
// 360 happened to get here, so count as another full rotation
TurnsCompleted += 1.0f;
// When turns complete, stop spinning and reset counter
if(TurnsCompleted>=TurnsPerActivation)
{
MainRotating = false;
TurnsCompleted = 0.0f;
}
}
My problem is with the method I am using to detect a complete turn.
My game revolves heavily around being able to accurately detect these full turns!
Does anyone have any possible solutions to this issue?
Thanks in advance.
Caution: I used to use C#, but for the last 18 months I’ve only coded javascript so that’s what my code will be.
private var fl_last_rotation : float;
function doRotate() {
// Rotate the main object
MyTransform.Rotate(0.0f, TurnSpeed, 0.0f);
if(MyTransform.eulerAngles.y <= fl_last_rotation)
{
// 360 happened to get here, so count as another full rotation
TurnsCompleted += 1.0f;
// When turns complete, stop spinning and reset counter
if(TurnsCompleted>=TurnsPerActivation)
{
MainRotating = false;
TurnsCompleted = 0.0f;
}
}
} else {
fl_last_rotation = MyTransform.eulerAngles.y;
}
That’s off the top of my head and may need improvement. But the concept is that the angle will reach 359 and then drop to 0 or 1 or 2. Therefore, if the current angle is less than the previous angle, it must have dropped, so it must have “passed” zero.
Ah I see now! I was trying to figure out myself how I could abuse the fact that it was going through a full cycle of rotation but couldn’t quite work it out.
That makes perfect sense, thank you very much. Problems all solved for now 
PS. Thank you for the coded example, it’s always helpful to see it in action.
Ail