Hi guys,
I’m trying to have a GameObject move using it’s rigidbody from a script. I seem to be unable to do it from another GameObject. This script is attached to “_GameController”:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary{
public float xMin, xMax, yMin, yMax;
}
public class PlayerMovement : MonoBehaviour {
public Boundary boundary;
public float tilt;
public float speed;
public Rigidbody player;
void Start(){
player = GameObject.Find("ship").GetComponent<Rigidbody>();
boundary.xMax = 8.5f;
boundary.xMin = -8.5f;
boundary.yMax = 4.5f;
boundary.yMin = -4.5f;
}
void FixedUpdate(){
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, moveVertical, 0.0f);
player.velocity = movement * speed;
player.position = new Vector3
(Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax), Mathf.Clamp(rigidbody.position.y, boundary.yMin, boundary.yMax), 0);
player.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
This script is meant to make “player” move. I know it’s setting the variable right but it’s telling me that “_GameController” has no rigidbody even though I’m not even trying to access it. Any ideas?
Now I know you will tell me to just put it on the Object i desire to move but the thing is, the object is a ship and the player can buy many ships with different speeds and everything so it would require a lot more work.