I’m trying to make it to where you can shoot asteroids and get points, but not of the tutorials make sense. Here’s a couple of my scripts.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Asteroid : MonoBehaviour
{
private Rigidbody2D rb2D;
private float revSpeed = 50.0f;
void Start ()
{
rb2D = gameObject.GetComponent<Rigidbody2D>();
Destroy(gameObject, 10);
}
void FixedUpdate ()
{
rb2D.MoveRotation(rb2D.rotation + revSpeed * Time.deltaTime);
}
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "Player")
{
SceneManager.LoadScene(0);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipControl : MonoBehaviour
{
public GameObject shotPrefab;
public Transform shotSpawn;
public float moveSpeed = 300f;
private Rigidbody2D shipHull;
private float screenWidth;
private float screenHeight;
public Vector2 newPosLeft;
public Vector2 newPosRight;
void Start ()
{
screenWidth = Screen.width;
screenHeight = Screen.height;
shipHull = gameObject.GetComponent<Rigidbody2D>();
newPosLeft = new Vector2(-3.0f, -2.8f);
newPosRight = new Vector2(3.0f, -2.8f);
}
void Update ()
{
int i = 0;
while (i < Input.touchCount)
{
if (Input.GetTouch(i).position.x > screenWidth / 2)
{
MoveShip(2.0f);
}
if (Input.GetTouch(i).position.x < screenWidth / 2)
{
MoveShip(-2.0f);
}
if (Input.GetTouch(i).position.y < screenHeight * 0.15f)
{
Fire();
}
++i;
}
}
void FixedUpdate()
{
#if UNITY_EDITOR
MoveShip(Input.GetAxis("Horizontal"));
if (Input.GetKeyDown(KeyCode.X))
{
Fire();
}
#endif
}
private void MoveShip(float horizontalInput)
{
shipHull.AddForce(new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0));
if(transform.position.x > 3.0f)
{
transform.position = newPosRight;
}
if(transform.position.x < -3.0f)
{
transform.position = newPosLeft;
}
}
void Fire()
{
var shot = (GameObject)Instantiate(shotPrefab, shotSpawn.position, shotSpawn.rotation);
shot.GetComponent<Rigidbody2D>().velocity = shot.transform.up * 6;
Destroy(shot, 2.0f);
}
}
Should I edit these, or will I need to make a new script enterly?