Hello community, I’m trying to build a script with the function OnTriggerEnter in C# that allows me to make a object that I choose rotate with a game controller when I’m in a trigger zone. The part of rotation in the script works, but when I try to merge it with the logic of trigger zone it doesn’t work. Also I get the following error: Assets/Scenes/Scripts/Rotator.cs(8,10): error CS1519: Unexpected symbol `if’ in class, struct, or interface member declaration.
If someone could help me with my scrip it woulb be great.
This is my script -
using UnityEngine;
using System.Collections;
public class Rotator : MonoBehaviour {
if (example==true)
{
public float rotationSpeed = 255f;
void Update () {
float rotationx = Input.GetAxis ("Rjoystick Horizontal") * rotationSpeed;
float rotationy = Input.GetAxis ("Rjoystick Vertical") * rotationSpeed;
// Rotate around our y-axis
print("The variable is :"+rotationy);
transform.Rotate (-rotationy, -rotationx, 0.0f);
}
}
}
void OnTriggerEnter(Collider other)
if (other.tag==OVRPlayerController) example==true
using UnityEngine;
using System.Collections;
public class Rotator : MonoBehaviour {
bool example;
public float rotationSpeed = 255f;
void Update () {
float rotationx = Input.GetAxis ("Rjoystick Horizontal") ;
float rotationy = Input.GetAxis ("Rjoystick Vertical") ;
if (exampletrue){
transform.Rotate (-rotationy * rotationSpeed, -rotationx * rotationSpeed, 0.0f);
print("The variable is :"+rotationy);
}
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "OVRPlayerController"){
example==true;
}
void OnTriggerExit(Collider col){
if (col.gameObject.tag == "OVRPlayerController"){
example==false;
}
}
}
i have edited it many times because of many wrong things you have done so let me explane somethings so you know what’s wrong to avoid in future :
you can’t use if out of functions but you can call functions inside if example :
void Update(){
if(something){
Test();
}
}
void Test(){
//do something
}
functions can’t bee outside the Class as you did with OnTriggerEnter function & tags should be inside Quotation Marks “OVRPlayerController”
lastly i have added a bool example deceleration you might missed & added OnTriggerExit function to set example to false so you can’t rotate when not collided .