Need help with advanced newbie programming script

Hi there, Unity3D community!

Since i know this community has very helpful users, i started this thread to get me helped with solving my scripting problem. Here it is:

// This script rotates an object if "Player" enters a trigger
var playerObject : GameObject;
var rotatingObject : GameObject;
var speed = Vector3(10,0,0);

function OnTriggerEnter (theTrigger : Collider) 
{
	if(theTrigger.gameObject == playerObject)
	{
		Debug.Log("Trigger started");	
		rotatingObject.transform.Rotate (speed * Time.deltaTime) = true;
	}
}

function OnTriggerExit (theTrigger : Collider) 
{
	if(theTrigger.gameObject == playerObject)
	{
		Debug.Log("Trigger stopped");
		rotatingObject.transform.Rotate (speed * Time.deltaTime) = false;
	}
}

I think the drawing explains what i mean. Sorry it’s so simple, had to do it in a hurry.
Any help would be appreciated. And once i know this one i’ll be able to share my knowledge with other newbies among us. So they thank you too. :slight_smile:

Rotation isn’t a boolean (true/false). You’d probably be better off putting your rotation inside function Update(). Then use OnTriggerEnter () to set the speed variable to some value and OnTriggerExit() to reset it to zero. Instead of setting speed to a Vector3, why not just set it to a float value, then define your rotation inside Update?

Also, just another thought. You might want to declare your rotatingObject variable as a Transform instead of GameObject:

var rotatingObject : Transform;

Rotation, position and size are handled by the transform component of the GameObject so you might as well save a bit of work and define it that way.

Your script might look like this:

var rotatingObject : Transform;
private var triggerHit : boolean = false;
var speed: float;

function Update()
{
if (triggerHit)
{
rotatingObject.Rotate (0, speed*Time.deltaTime,0);
}
}

function OnTriggerEnter(hit : Collider)
{
if (hit.gameObject.tag == "Player")
{
triggerHit = true;
}
}

function OnTriggerExit ()
{
triggerHit = false;
}

Haven’t tried this out, but you get the idea :slight_smile:

Thank you sooo much for a fast respond. It works great, thank you tool55!

Further upgrades and problems on the script will be posted here so whoever needs this kind of scripts can always come to this thread and get their problems solved.