this is probably a common problem that i will say “oh duh” to once its solved. but i keep getting a reference error when trying to access the magnitude of the rigidbody of another game object. my code for the object being referenced looks like this:
PlayerController.cs
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public float jump;
public Rigidbody rb;
private bool isGrounded;
public GameObject target;
// target is the object you will take the rotations from
void Start () {
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
if (isGrounded == true) {
rb.AddForce (moveVertical * target.transform.forward * speed);
rb.AddForce (moveHorizontal * target.transform.right * speed);
if (Input.GetKeyDown ("space")) {
rb.AddForce (0.0f, jump, 0.0f);
}
}
else {
rb.AddForce (moveVertical * target.transform.forward * speed * 0.5f);
rb.AddForce (moveHorizontal * target.transform.right * speed * 0.5f);
}
}
void OnCollisionStay(Collision collision) {
if (collision.gameObject.tag == "Ground") {
isGrounded = true;
//print ("Grounded");
}
}
void OnCollisionExit (Collision collision) {
if (collision.gameObject.tag == "Ground") {
isGrounded = false;
//print ("Not Grounded");
}
}
}
the script that im referencing the rigidbody in looks like this: gyroScript.cs
using UnityEngine;
using System.Collections;
public class gyroScript : MonoBehaviour {
public GameObject target;
public float speed = 10f;
public float rotationSpeed = 100.0f;
private Vector3 offset;
public PlayerController follower;
public float giveaway;
public float playerSpeed;
void Start () {
offset = transform.position - target.transform.position;
follower = GetComponent<PlayerController> ();
}
void Update()
{
mouseOrbit();
}
void LateUpdate () {
transform.position = target.transform.position + offset;
//***
playerSpeed = follower.rb.velocity.magnitude;
// this is where i get my error***
print (playerSpeed);
}
void mouseOrbit()
{
float translation = Input.GetAxis("Mouse Y") * speed;
float rotation = Input.GetAxis("Mouse X") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
}
}
ive checked all my references to ensure that they are correct, infact i actually took that code directly from code i placed onto a camera object that worked without error that looked like this: CameraController.cs
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public PlayerController target;
public float giveaway;
float playerSpeed;
void start(){
target = GetComponent<PlayerController>();
}
void Update (){
playerSpeed = target.rb.velocity.magnitude; //same codebit that causes issues in other script
print (playerSpeed);
transform.position = new Vector3 (0f, 1.25f, playerSpeed * giveaway *-1);
print (transform.position);
}
}
i aplologize for the long reads, but i just cant seem to figure out where im going wrong in this script, everything seems like it should work in theory
any help is much appreciated!