Hi, Extremely new to unity and c#
I’m following the Ruby’s Adventure 2D tutorial and have just wrote the script for launching a projectile when triggered ( by pressing “c” on a keyboard" , that works fine apart from the fact that 5 seconds after i press play in the editor a projectile that hasn’t been launched goes on to hit a robot and for some unknown reason completely removes the tilemap in game.
The projectile is linked to the character “ruby” as she launches it here’s the scripts i’ve used.
Projectile
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile : MonoBehaviour
{
Rigidbody2D rigidbody2d;
void Awake()
{
rigidbody2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
public void Launch(Vector2 direction, float force)
{
rigidbody2d.AddForce(direction * force);
}
void OnCollisionEnter2D(Collision2D other)
{
//we also add a debug log to know what the projectile touch
Debug.Log("Projectile Collision with " + other.gameObject);
Destroy(gameObject);
}
}
RubyController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RubyController : MonoBehaviour
{
public float speed = 3.0f;
public int maxHealth = 5;
public float timeInvincible = 2.0f;
public GameObject projectilePrefab;
int currentHealth;
bool isInvincible;
float invincibleTimer;
public int health { get { return currentHealth; } }
Rigidbody2D rigidbody2d;
Animator animator;
Vector2 lookDirection = new Vector2(1, 0);
// Start is called before the first frame update
void Start()
{
rigidbody2d = GetComponent<Rigidbody2D>();
currentHealth = maxHealth;
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
Launch();
}
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector2 move = new Vector2(horizontal, vertical);
if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
{
lookDirection.Set(move.x, move.y);
lookDirection.Normalize();
}
animator.SetFloat("Move X", lookDirection.x);
animator.SetFloat("Move Y", lookDirection.y);
animator.SetFloat("Speed", move.magnitude);
Vector2 position = rigidbody2d.position;
position = position + move * speed * Time.deltaTime;
rigidbody2d.MovePosition(position);
if (isInvincible)
{
invincibleTimer -= Time.deltaTime;
if (invincibleTimer < 0)
isInvincible = false;
}
}
public void ChangeHealth(int amount)
{
if (amount < 0)
{
if (isInvincible)
return;
isInvincible = true;
invincibleTimer = timeInvincible;
animator.SetTrigger("Hit");
}
currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
Debug.Log(currentHealth + "/" + maxHealth);
}
void Launch()
{
GameObject projectileObject = Instantiate(projectilePrefab, rigidbody2d.position + Vector2.up * 0.5f, Quaternion.identity);
Projectile projectile = projectileObject.GetComponent<Projectile>();
projectile.Launch(lookDirection, 300);
animator.SetTrigger("Launch");
}
}
Hopefully someone can help me out as the tutorial is kinda buggy and some stuff doesnt actually work as it should