Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_vcA1C6z_9wAK-A · May 05, 2019 at 08:35 PM · 2d gametilemapprojectile

5 seconds after i hit play a projectile that hasnt been triggered launches and removes the tilemap, HELP

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 :(

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SamohtVII · May 06, 2019 at 01:38 AM

Did you delete the original projectile prefab so there are none at the start of your scene? What do you mean by "tilemap", what is that?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_vcA1C6z_9wAK-A · May 06, 2019 at 09:28 AM 0
Share

Yeah deleted from the original scene the projectile is attached to the character ruby and can only be launched when the "c" key is pressed on a keyboard. So when the scene is played the projectile gameobject isnt present at all.

Like the tiles used to create the background for the scene.

avatar image
0

Answer by Tomashu · May 06, 2019 at 11:25 AM

You haven't, accidentally, put the Projectile script onto the Tilemap (or somewhere else it isn't supposed to be)?

Throwing some logic at it:

  • You did not launch a Projectile gameobject, so it is not likely to actually be one of those.

  • The Projectile script destroys the gameobject it is attached to - which appears to be the tilemap (or an ancestor).

  • Collissions work both ways - so it could be that the robot is what is moving.


Try to add the name of the gameobject the Projectile script is added to in the Debug.Log() output and change the code to:

 void OnCollisionEnter2D(Collision2D other)
 {
     //we also add a debug log to know what the projectile touch
     Debug.Log(this.gameObject + " collided with " + other.gameObject);

     Selection.SetActiveObjectWithContext(this.gameObject, this);
     EditorApplication.ExecuteMenuItem("Edit/Frame Selected");
     EditorApplication.isPaused = true;
     // Destroy(gameObject);
  }

So that you can have a look at it happening in the editor.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with 2d Civ style map 0 Answers

Raycasting not working as expected: rays are perpendicular to projectile's trajectory and collision doesn't occur 1 Answer

Why my projectile is passing through the tilemap? How can I solve this easily? 2 Answers

Anyone knows how to refresh tiles? 0 Answers

Generating a 2D map out of a tileset problem. Array and If statements. HELP! 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges