JAVA SCRIPT TO C# CONVERSION

iam learning a fps game, where i have two scripts 1.mouselookupscript for camera and 2.playermovementscript.

i want to access a variable currentyrotation from the mouselookupscript,but i get an error message regarding the declaration of the script. below are the scripts.

this is a tutorial which is in javascript, but am trying to do it in c#. need help.

MouseLookUpscript

using UnityEngine;
using System.Collections;

public class mouse_lookupScript : MonoBehaviour {
	
	float lookSensitivity = 2f;
	float xRotation;
	float yRotation;
	float currentXrotation;
	float currentYrotation;
	float xRotationv;
	float yRotationv;
	float lookSmoothDamp = 0.03f;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		xRotation += Input.GetAxis("Mouse Y") * lookSensitivity;
		yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
		
		xRotation = Mathf.Clamp(xRotation, -90, 90);
		
		currentXrotation = Mathf.SmoothDamp(currentXrotation, xRotation, ref xRotationv, lookSmoothDamp);
		currentYrotation = Mathf.SmoothDamp(currentYrotation, yRotation, ref yRotationv, lookSmoothDamp);
		
		transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
	}
}

PlayerMovementscript

using UnityEngine;
using System.Collections;

public class player_movement : MonoBehaviour {
	
	public float walkAccleration = 10f;
	public GameObject cameraObject;
	

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent<mouse_lookupScript>().currentYrotation, 0);  		
	
	}
}

the last line transform.rotation is where i get error. i couldn’t able to access the currentYrotation variable. need help.

The way I do my script accessing is like this : (Copied from one of my scripts)

//Public Variables
public GameObject globalScripts;
//Private Variables
private bool ps3controller;

void Start()
{
        globalScripts = GameObject.FindGameObjectWithTag("Global");
        PS3MenuNavigation PS3Script = globalScripts.GetComponent<PS3MenuNavigation>();
        ps3controller = PS3Script.GetPS3State();
}

In your code I would create a function that returns currentXrotation and one that returns currentYrotation

public float GetCurRotX()
{
      return currentXrotation;
}

public float GetCurRotY()
{
      return currentYrotation;
}

Then declare the object your MouseLookUpscript is attatched to

//Public Variables
public GameObject cameraObject;

(You can drag it in via the unity editor or you can tag tha object and find it via script)

//If the tag was MainCamera
cameraObject = GameObject.FindGameObjectWithTag("MainCamera");

Now find the script attached to the object

mouse_lookupScript MLUScript = cameraObject.GetComponent<mouse_lookupScript>();

Now either access the variable directly if it is public or access the method to get the private variable

rotX = MLUScript.GetCurRotX();
rotY = MLUScript.GetCurRotY();

rotX = MLUScript.currentXrotation;
rotY = MLUScript.currentYrotation;

Hope this helps :slight_smile: