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 /
avatar image
0
Question by akashisayaki · Feb 03, 2021 at 05:07 PM · unity 2dforcegame developmentobstacle

Constantly add force and throw an object in every 2 seconds!

Hi, I want to know if there's any methods to add force and throw an object without using any buttons but with seconds? Every 2 seconds the ball will add force and launch itself towards the player. I want to make it as an obstacle for my game. This is the code for now: public class EnemyBouncer : MonoBehaviour {

 public float speed;
 public float stoppingDistance;
 public float retreatDistance;

 public Transform player;

 void Start()
 {
     player = GameObject.FindGameObjectWithTag("Player").transform;
 }

 void Update()
 {
     if (Vector2.Distance(transform.position, player.position) > stoppingDistance)
     {
         transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
     }

     else if (Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > retreatDistance)
     {
         transform.position = this.transform.position;
     }

     else if ((Vector2.Distance(transform.position, player.position) < retreatDistance))
     {
         transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
     }
 }

}

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 Llama_w_2Ls · Feb 03, 2021 at 06:48 PM

You can use a coroutine to do this. For example:

 void Start()
 {
      StartCoroutine(FireObject(2));
 }
 
 IEnumerator FireObject(float delay)
 {
      // Waits 'delay' amount of seconds
      yield return new WaitForSeconds(delay);
 
      // Fires object
      Fire();
 }

@akashisayaki

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
avatar image
0

Answer by afraism · Feb 03, 2021 at 07:05 PM

This can be done with InvokeRepeating

 using UnityEngine;
 using System.Collections.Generic;
 
 // Starting in 2 seconds.
 // a projectile will be launched every 0.3 seconds
 
 public class ExampleScript : MonoBehaviour
 {
     public Rigidbody projectile;
 
     void Start()
     {
         InvokeRepeating("LaunchProjectile", 2.0f, 0.3f);
     }
 
     void LaunchProjectile()
     {
         Rigidbody instance = Instantiate(projectile);
 
         instance.velocity = Random.insideUnitSphere * 5;
     }
 }

public void InvokeRepeating(string methodName, float time, float repeatRate);

check this link

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

116 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

Related Questions

Chat-based text adventure game 0 Answers

How generate a 2D picture puzzle from a sprite visible on game scene 1 Answer

Reuse player's script for the enemy 1 Answer

how to make an object move in a rectangle path ? 1 Answer

scaling error for objects in android 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