using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, yMin, yMax; // The limit values of Player's moves(forward,back,left,right)
}
public class Movement : MonoBehaviour
{
public float moveSpeed; // Player move speed
public Boundary boundary; // Objective variable for Boundary
public Rigidbody2D rb; // Objective variable for Rigidbody2D
public GameObject shot; // shot prefeb
public Transform ShotPoint; // shot start
public float fireRate = 0.5f; // shot term
private float nextFire = 0.0f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
// Set the Player's Limited move sections
GetComponent<Transform>().position = new Vector2(Mathf.Clamp(GetComponent<Transform>().position.x, boundary.xMin, boundary.xMax), Mathf.Clamp(GetComponent<Transform>().position.y, boundary.yMin, boundary.yMax));
// Set the Player's movements options in this game, Just Left and Right
// Right
if (Input.GetKeyDown(KeyCode.RightArrow))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, 0);
}
// Left
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, 0);
}
nextFire = Time.time + fireRate;
Instantiate(shot, ShotPoint.position, ShotPoint.rotation);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotPlayer : MonoBehaviour
{
public float speedBullet;
// Use this for initialization
void Start()
{
GetComponent<Rigidbody2D>().velocity = transform.up * speedBullet;
}
// Update is called once per frame
void Update()
{
}
}
Also, put in a Debug.Break() call when you spawn the bullet, then go looking through your scene to see if the bullet is present.
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
Also if you’re using physics and spawn the bullet “inside” the player, it could be getting ejected at massive velocities and flying far away before you see it.
If this is happening, one approach is to use the physics system layers to specify the player layer and bullet layers should not collide.
UnassignedReferenceException: The variable Shot of Movement has not been assigned.
You probably need to assign the Shot variable of the Movement script in the inspector.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem: