Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by antonioolive2001 · Oct 01, 2020 at 12:51 AM · triggertriggersprojectileturretturrets

How to make a turret with OnTriggerEnter

Hi there! I've just started coding and i'm trying to make a dungeon-like game. I wanted to make a trap that when the player enters a trigger, an arrow is fired at him. I don't want to use any AI, just a arrow firing straight forward. I already made the sprite and have tried using Instantiate, but i don't really know the syntax. Thx! The only code i have is a projectile script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Projectile : MonoBehaviour
 {
    Rigidbody2D rigidbody2d;
    public float projectileDistance;
 
     void Awake()
     {
         rigidbody2d = GetComponent<Rigidbody2D>();
     }
 
     void Update() {
         if(transform.position.magnitude > projectileDistance){
             Destroy(gameObject);
         }
     }
 
     public void Launch(Vector2 direction, float force){
         rigidbody2d.AddForce(direction * force);
     }
 
     void OnCollisionEnter2D(Collision2D other) {
         Debug.Log("projectile collision with " + other.gameObject);
         Destroy(gameObject);
     }
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Oct 01, 2020 at 04:16 AM

You just need to call Launch from within an OnTriggerEnter event handling function in script that is a component of your trap object.

Assuming "straight forward" is your projectile's +x axis, use Vector3.forward for the direction parameter of your launch script.

To call Launch, your trap script will need a reference to the projectile script - it's not clear if you are spawning the projectile dynamically or it already exists in your scene. Either way you need a variable that references the projectile to be able to call Launch

This bit of code has some problems:

     void Update() {
          if(transform.position.magnitude > projectileDistance){
              Destroy(gameObject);
          }



First, you are dealing with a Rigidbody so it is better to use FixedUpdate; and also rigidbody2d.position instead of transform.position. It is better to do that in FixedUpdate because the rigidbody motion is calculated by the physics engine, and Update may not be in sync with the engine timing. While it is OK in your circumstance to use transform.position to get the position of something, I'd use the rigidbody instead just as a matter a habit. If you are using rigidbody motion, it is very important not to set transform.position.

Secondly and most important: position.magnitude is not meaningful - it only means something if starting position of your projectile is (0,0) You need to get the distance traveled from it's starting position. So (current position - start position).magnitude

Lastly, as a matter of practice getting the magnitude is an expensive operation: better to use sqrMagnitude and compare it with the square of your projectileDistance - precompute the square of projectileDistance when your game starts and store it in a variable to use in the comparison.

Comment
Add comment · Show 2 · 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 antonioolive2001 · Oct 01, 2020 at 06:44 PM 0
Share

Thx! I want to spawn a prefab of an arrow, I don't need to use instantiate or something like that besides calling Launch?

avatar image streeetwalker antonioolive2001 · Oct 01, 2020 at 06:48 PM 0
Share

If the arrow is not already in the scene, you have to use Instantiate the prefab to "spawn" it. That is usually what is meant by spawning an object - to create an object. You have to do that using Instantiate before you can call a function on a script component of the object.

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

236 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 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

Trigger Parameters not working inside Animator Trigger action 0 Answers

iTweenEditor Make itween start from a trigger 0 Answers

Problem With Triggers? 0 Answers

C# Activate Object on Collision with tag not working. 1 Answer

Bug? or nuance with trigger detection? 1 Answer


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