How do I simulate relative movement?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class AsteroidMover : MonoBehaviour {
public int speed;
Rigidbody rb;
GameObject go = GameObject.Find("Player");
//Trying to grab the Player object's info - in this case the ship in the center of the screen
void Start ()
{
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate ()
{
if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.UpArrow))
{
rb.velocity += go.transform.forward * -speed * Time.deltaTime;
}
}
}
I've got a PlayerController script elsewhere that handles the top-down Y-axis rotation of the ship, and - when the player presses W or UpArrow - lights up the forward thrusters' particle effects. The objective with this code is to have the ship not actually move. Instead all other objects alter their velocity based on your rotation and how long you're firing your rockets in that direction.
Right now, it just...doesn't. It doesn't throw any errors until the program is running and you try to move, at which point it repeatedly says "Object reference not set to an instance of an object"
What am I doing wrong?
Your answer
Follow this Question
Related Questions
I have prefabs that should be referencing different GameObjects, but they all refer to the same one. 0 Answers
can you control 2 gameobject color values without having to reference both ? 0 Answers
Cannot figure out GetComponent for the life of me 1 Answer
Creating depth for background objects 0 Answers