Problem with my JS, Can't rotate Cube!

So I am fairly new to JS and need some help I created this little script to rotate the object every time my 3rd person player Collider runs through certain boxes, however the code doesn’t activate, I need someone to look at at and help me out a bit. Its a very simplistic code and I feel if I would have done this is C# I wouldn’t run into this problem, however I am forcing myself to learn JS.

#pragma strict
public var player : Collider;
private var rotating : boolean = false;
public var rotateSpeed : int = 1;
private var leftRotate : boolean = false;
private var rightRotate : boolean = false;
public var colliderL : Collider;
public var colliderR : Collider;

function OnCollisionEnter(player : Collision)
{
	if (colliderL == player)
	{
		leftRotate = true;
	}
	if (colliderR == player)
	{
		rightRotate = true;
	}
}

function LevelRotate()
{
 
	if (rightRotate == true)
	{
   		if (!rotating)
   		{

      		rotating = true;

      		var curRot : float = 0;
      		var startRot : float = transform.eulerAngles.z;

      		while (curRot < 90) 
      		{

         		curRot += rotateSpeed;

         		transform.eulerAngles.z = startRot + curRot;

         		yield;

      		}

      		transform.eulerAngles.z = Mathf.Round(startRot + 90);

      		rotating = false;
      		rightRotate = false;
      	}
      }
     
    if (leftRotate == true)
	{
   		if (!rotating)
   		{

      		rotating = true;

      		var curRot2 : float = 0;
      		var startRot2 : float = transform.eulerAngles.z;

      		while (curRot2 > -90) 
      		{

         		curRot2 += rotateSpeed;

         		transform.eulerAngles.z = startRot2 - curRot2;

         		yield;

      		}

      		transform.eulerAngles.z = Mathf.Round(startRot2 - 90);

      		rotating = false;
      	}
      	leftRotate = false;
      }
}

"I feel if I would have done this is C# I wouldn't run into this problem, however I am forcing myself to learn JS" - Just out of interest, why are you making yourself use Unityscript if you already know C#? Most people regard C# as a "superior" language, and it's certainly a more transferrable skill...?

That confused me too. It's not even legit JS. It isn't a crossover skill outside of Unity.

Should I just stick to C#? I'm finding this language to fight me at every turn unlike C#.

I use and prefer C#. It's lovely.

1 Answer

1

I don’t see you calling LevelRotate() anywhere… That’s an issue.

What do you mean I didn't call levelrotate anywhere? didn't I already solve that issue by making the vars global to all the functions?

There's a big difference between the accessibility of a member or method (public/protected/private), and whether or not that method is ever actually called. Is something executing the function LevelRotate()?