Question by
sanderschellekens · Apr 17, 2016 at 12:23 PM ·
textissueuser interfacespeedometer
CS0122 C# is inaccessible due to its protection level when accessing PlayerController.rb......
Hey guys, i'm trying to make a simple speed/velocity meter, I found out i could do so by accesing the rigidbody's velocity.magnitude. Only problem is, the rigidbody belongs to the player gameobject rather than the textUI object.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Speedmeter : MonoBehaviour
{
public float Speed;
public GameObject CuboidPlayer;
public Rigidbody rb;
public Text text;
void Awake()
{
text = GetComponent<Text>();
}
void Start()
{
GameObject.Find("CuboidPlayer");
Speed = PlayerController.rb.velocity.magnitude;
}
void Update()
{
text.text = " " + PlayerController.rb.velocity.magnitude;
}
}
It presents me with said error and an unworking project with this playercontroller script: ( unworking as in nothing responds anymore while there are no errors or whatsoever except the one mentioned ).
using UnityEngine;
using System.Collections;
[System.Serializable]//This adds another class to the script menu in the inspector tab.
public class Boundary
{
public float xMin, xMax, yMin, yMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public GameObject attractedTo;// Set's gravity pull towards star
public float strengthOfAttraction = 5.0f;
//The purpose of this script is to simulate Newtonian phy
public float maxThrust = 0.01f; //The maximum Thrust provided by the thruster(s) at full throttle
public float rollWeight = 0.000000000005f; //This float and the next two only serve to adjust sensitivity
public float pitchWeight = 0.000000000005f;//of the controls, and to allow calibration for more massive ships.
public float yawWeight = 0.00000000005f;//Set these 3 floats to the mass of the rigidbody for sensitive controls
public AudioClip ImpulseSound;
public Boundary boundary;
public float speed;
private bool isMoving;
bool isPlaying;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
isMoving = (Input.GetKeyDown("z") || Input.GetKeyDown("x") || Input.GetKeyDown("left") || Input.GetKeyDown("right") || Input.GetKeyDown("up") || Input.GetKeyDown("down"));
}
void Update()
{
speed = rb.velocity.magnitude;
//The code below describes when to play the sound to tell you are moving.
if (isMoving)
{
GetComponent<AudioSource>().clip = ImpulseSound;
GetComponent<AudioSource>().Play();
}
else
{
GetComponent<AudioSource>().clip = ImpulseSound;
GetComponent<AudioSource>().Stop();
}
transform.position = new Vector3 //Here I clamp the cuboid's position within the Cubiverse sector's boundaries
(
Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp(GetComponent<Rigidbody>().position.y, boundary.yMin, boundary.yMax),
Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
}
void FixedUpdate()
{
Vector3 direction = attractedTo.transform.position - transform.position;//Sets values for gravity pull towards star
GetComponent<Rigidbody>().AddForce(strengthOfAttraction * direction);
float yaw = yawWeight * Input.GetAxis("Yaw") * Time.deltaTime;//Movement and rotation on 3 axis'
float roll = rollWeight * Input.GetAxis("Roll") * Time.deltaTime;
float pitch = pitchWeight * Input.GetAxis("Pitch") * Time.deltaTime;
Vector3 Rotation = new Vector3(pitch, roll, yaw);
rb.AddRelativeTorque(Rotation);
float throttle = maxThrust * Input.GetAxis("Thrust");
rb.AddRelativeForce(Vector3.forward * throttle);
System.Console.WriteLine("input is " + yaw.ToString() + ", " + pitch.ToString() + ", " + roll.ToString());
float strafeHor = maxThrust * Input.GetAxis("StrafeHorizontal");
rb.AddRelativeForce(Vector3.left * strafeHor);
float strafeVer = maxThrust * Input.GetAxis("StrafeVertical");
rb.AddRelativeForce(Vector3.up * strafeVer);
}
}
after trying to make the public float speed in the PlayerController script static it all went wrong. I intentionally didn't try a static variable at first because that seemed, static rather than dynamic which i want for my ship. I want the number to go up or down a long the values presented by my cuboid's rigidbody.
Comment