Hi all, I’m new to unity and coding. I’m trying to make this game that based if the player is looking up, down, left or right he will shoot at that direction according to the animation. (It’s a top-down 2D game) Here’s the script for the animation: (which is also the movements as well)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float moreSpeed;
private float speed = 0.02f;
public Animator anim;
public bool upAnim;
public bool downAnim;
public bool leftAnim;
public bool rightAnim;
public void MoreMovement()
{
// --------------- Extra Speed Movements ----------------
if (Input.GetKey(KeyCode.UpArrow) && (Input.GetKey(KeyCode.Space)))
{
transform.Translate(0,speed + moreSpeed, 0);
}
if (Input.GetKey(KeyCode.DownArrow) && (Input.GetKey(KeyCode.Space)))
{
transform.Translate(0,-speed - moreSpeed, 0);
}
if (Input.GetKey(KeyCode.RightArrow) && (Input.GetKey(KeyCode.Space)))
{
transform.Translate(speed + moreSpeed, 0, 0);
}
if (Input.GetKey(KeyCode.LeftArrow) && (Input.GetKey(KeyCode.Space)))
{
transform.Translate(-speed - moreSpeed, 0, 0);
}
}
public void Movement() {
// ---------------Normal Movements---------------
if (Input.GetKey(KeyCode.UpArrow))
{
// xUp = xUp + speed;
transform.Translate(0, speed, 0);
anim.SetBool("Up", true);
anim.SetBool("Down", false);
anim.SetBool("Left", false);
anim.SetBool("Right", false);
upAnim = true;
downAnim = false;
leftAnim = false;
rightAnim = false;
}
if (Input.GetKey(KeyCode.DownArrow))
{
// xDown = xDown - speed;
transform.Translate(0, -speed, 0);
anim.SetBool("Up", false);
anim.SetBool("Down", true);
anim.SetBool("Left", false);
anim.SetBool("Right", false);
upAnim = false;
downAnim = true;
leftAnim = false;
rightAnim = false;
}
if (Input.GetKey(KeyCode.RightArrow))
{
// yUp = yUp + speed;
transform.Translate(speed, 0, 0);
anim.SetBool("Up", false);
anim.SetBool("Down", false);
anim.SetBool("Left", false);
anim.SetBool("Right", true);
upAnim = false;
downAnim = false;
leftAnim = false;
rightAnim = true;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
// yDown = yDown - speed;
transform.Translate(-speed, 0, 0);
anim.SetBool("Up", false);
anim.SetBool("Down", false);
anim.SetBool("Left", true);
anim.SetBool("Right", false);
upAnim = false;
downAnim = false;
leftAnim = true;
rightAnim = false;
}
}
void Start () {
anim = GetComponent<Animator>();
}
private void FixedUpdate()
{
//we always use FixedUpdate for physics (rigidbody2d)
{
Movement();
MoreMovement();
}
}
// Update is called once per frame
void Update () {
}
}
Based if the player is looking up, down, left or right, an appropriate bool will be set to true, while the others to false. Now I’ll wanna use getcomponent on my prefab beam1 (which is the beam which is being shot) so get the information on the PlayerMovement scripts to the prefab script. But it doesn’t quite work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BeamPrefabShooting : MonoBehaviour {
public Rigidbody2D rb;
public float bulletSpeed;
public GameObject collidedBullet;
private PlayerMovement playerMovement;
public GameObject beam1;
private void Start()
{
playerMovement = beam1.GetComponent<PlayerMovement>();
}
void FixedUpdate () {
if (playerMovement.rightAnim == true)
{
rb.velocity = new Vector2(bulletSpeed, 0);
}
if (playerMovement.leftAnim == true)
{
rb.velocity = new Vector2(-bulletSpeed, 0);
}
if (playerMovement.upAnim == true)
{
rb.velocity = new Vector2(0, bulletSpeed);
}
if (playerMovement.downAnim == true)
{
rb.velocity = new Vector2(0, -bulletSpeed);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "wall")
{
Destroy(collidedBullet);
}
}
}
The beam can only be shot if the player has picked up the beam from the ground (if the gameobject has been destroyed)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BeamShooting : MonoBehaviour
{
public GameObject destroyedBeam;
public GameObject beam;
public Transform bulletSpawn;
void Update()
{
BeamShoot();
}
public void BeamShoot()
{
if (destroyedBeam = destroyedBeam)
{
}
else
{
if (Input.GetKeyDown(KeyCode.E))
{
Instantiate(beam, bulletSpawn.transform);
}
}
}
}
Here’s the pickup script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BeamPickUp : MonoBehaviour {
public Object Player;
public Object pickUpBeam;
public int itemCount;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
afterDestroyed();
}
void OnCollisionEnter2D(Collision2D Player)
{
if (Player.gameObject.tag == "Beam" )
{
Destroy(pickUpBeam);
itemCount++;
print("Item count = " + itemCount);
}
}
void afterDestroyed()
{
/* if (pickUpBeam = pickUpBeam)
{
print("not destroyed");
}
else
{
print("destroyed");
}
*/}
}
Thank you so much. I’m certain that the error is about unity not being able to correctly transfer the playermoevment script to the prefab script but I added the rest of the scripts that are related just in case. I know it’s a bit of a mess, but I’m slowly learning and correcting everything as well on the way.
Thank you!!!
EDIT: This is the error I get:
NullReferenceException: Object reference not set to an instance of an object
BeamPrefabShooting.FixedUpdate () (at Assets/BeamPrefabShooting.cs:17)