FIRE LOGIC
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine;
public class FireLogic : MonoBehaviour
{
{
public Camera ourCamera;
public GameObject cannonBallPrefab;
public GameObject cannonBallPrefab;
private Vector3 ballStartPos = new Vector3(101, 9, 55);
private GameObject selectedBall;
private GameObject selectedBall;
public GameObject cannon;
// Start is called before the first frame update
void Start()
{
}
void Start()
{
}
// Update is called once per frame
void Update()
{
Ray ourRay = ourCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.GetMouseButtonDown(0) && Physics.Raycast(ourRay, out hit))
{
string tag = hit.collider.tag;
Debug.Log(tag);
if (tag.Equals("Player"))
{
Debug.Log("correct tag");
ballStartPos = new Vector3 (cannon.transform.position.x, cannon.transform.position.y, cannon.transform.position.z + 5) ;
selectedBall = Instantiate(cannonBallPrefab, ballStartPos, Quaternion.identity);
selectedBall.GetComponent<Rigidbody>().AddForce(new Vector3(0, 10, 4000));
void Update()
{
Ray ourRay = ourCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.GetMouseButtonDown(0) && Physics.Raycast(ourRay, out hit))
{
string tag = hit.collider.tag;
Debug.Log(tag);
if (tag.Equals("Player"))
{
Debug.Log("correct tag");
ballStartPos = new Vector3 (cannon.transform.position.x, cannon.transform.position.y, cannon.transform.position.z + 5) ;
selectedBall = Instantiate(cannonBallPrefab, ballStartPos, Quaternion.identity);
selectedBall.GetComponent<Rigidbody>().AddForce(new Vector3(0, 10, 4000));
}
}
}
}
}
}
}
CANNON MOVEMENT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine;
public class CannonMovement : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
{
// Start is called before the first frame update
void Start()
{
}
public float speed = 5f;
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Debug.Log(x + " " + z);
Vector3 movement = new Vector3(-z, 0, x);
transform.Translate(movement * speed * Time.deltaTime);
}
}
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Debug.Log(x + " " + z);
Vector3 movement = new Vector3(-z, 0, x);
transform.Translate(movement * speed * Time.deltaTime);
}
}
SCENE LOGIC
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SceneLogic : MonoBehaviour
{
public Button startButton;
{
public Button startButton;
// Start is called before the first frame update
void Start()
{
startButton.onClick.AddListener(switchScene);
}
void Start()
{
startButton.onClick.AddListener(switchScene);
}
// Update is called once per frame
void Update()
{
}
void switchScene()
{
SceneManager.LoadScene("MainScence");
}
}
void Update()
{
}
void switchScene()
{
SceneManager.LoadScene("MainScence");
}
}
Steps:
1. I created the terrain and added a grassy material onto it. Using the "paint terrain" function I added some texture to it so that the terrain looked hillier and more natural.
2. I downloaded the cannon and box assets from the Asset Store. I populated the scene and stacked the boxes so they can be knocked over.
3. I added logic to instantiate the cannon balls and played around with the forces until it fired how I envisioned.
4. I made it so that I could move the cannon around to hit more boxes by adding a script to the cannon itself. Additionally, in the FireLogic script, I referenced the position of the cannon to calculate where to spawn the new cannon ball.
5. Finally I created the StartScene. I added a canvas, text box, and. button. I used LoadScene to load the MainScene when the button is pressed.