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 Arcticstar · Sep 08, 2017 at 07:19 AM · unity 5scripting problemmovement

Please Check the code and tell me why this isn't working?!

so I'm doing an artillery game, so where the shell hit near the tank I want it to be tracked (stop moving for 2 seconds) which it works fine, but the problem is it never moves again no matter how many attempts and codes I tried to assign the speed to 1f again it never works. here are the codes

the code on the shell which it hits and cause the tracking.

using UnityEngine; using System.Collections;

public class Tracked : MonoBehaviour {

 public float FarAreaEffectt;
 private Transform explosion;
 private Transform enemy;
 public static bool wasTracked = false;

 void Awake() {
     explosion = transform;
 }
 void OnCollisionEnter () {

     Collider[] colls = Physics.OverlapSphere (explosion.position, FarAreaEffectt);

     foreach(Collider col in colls) {

         if(col.CompareTag("Enemy")) {

             float distance = Vector3.Distance (col.transform.position, explosion.position);

                 tracked (col.transform);
             Debug.Log ("enemy tracked");

             Invoke ("move", 2f);
             }
     }
 }

 void tracked(Transform enemy) {
     enemymove em = enemy.GetComponent<enemymove> ();
     em.speed = 0f;
 }

 void OnDrawGizmosSelected() {

     Gizmos.color = Color.red;
     Gizmos.DrawWireSphere (transform.position, FarAreaEffectt);
 }

 public void move(Transform enemy){
     Debug.Log ("invoked");
     enemymove e = enemy.GetComponent<enemymove> ();
     e.speed = 1f;
 }

}

and here is the enemyMovement secript

using UnityEngine;

public class enemymove : MonoBehaviour {

 public float speed;

 private Transform target;
 private int wavepointIndex = 0;

 void Start () {


     target = WayPoints.points [0];
 }

 void Update() {
     

     Vector3 dir = target.position - transform.position;
     Quaternion rot = Quaternion.LookRotation (dir);

     transform.rotation = Quaternion.Slerp (transform.rotation, rot, speed * Time.deltaTime);

     transform.Translate (dir.normalized * speed * Time.deltaTime, Space.World);

 

     if (Vector3.Distance (transform.position, target.position) <= 0.4f) {

         GetNextWayPoint ();

     }
     }

 void GetNextWayPoint() {

     if (wavepointIndex >= WayPoints.points.Length - 1) {

         speed = 0f;

         return;
     }

     wavepointIndex++;
     target = WayPoints.points [wavepointIndex];
 }

}

so what I'm doing wrong? is it because the shell get destroyed after the first time it get collides with the terrain and cause the tracking?

please help.

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 FlaSh-G · Sep 08, 2017 at 09:31 AM

is it because the shell get destroyed after the first time it get collides with the terrain and cause the tracking?

Yes. If there is something destroying the object that has Invoke waiting to call a method on it, Invoke can't finish its job. It's much better to re-enable movement on the stuck object itself, because of that and because it makes more sense in a semantic way (as in why would the bullet free the vehicle again?).

Generally, in Unity, try to have things done by the objects that would do it in real life.

In code, this would mean that you add one or two methods to your vehicle script:

 public void GetStuckFor(float time)
 {
   StartCoroutine(StuckCoroutine(time));
 }
 
 private IEnumerator StuckCoroutine(float time)
 {
   var originalSpeed = speed;
   speed = 0;

   yield return new WaitForSeconds(time);

   speed = originalSpeed;
 }

I replaced Invoke with a coroutine here, which doesn't rely on a string to identify the method to call. It also means that setting the speed, waiting, and setting it back to normal are all lines of one and the same method rather than splitting it into multiple methods, like with Invoke.

Of course, if you prefer to use Invoke, you can still do that here.

Either way, the bullet script just calls the new method now:

 col.GetComponent<EnemyMove>().GetStuckFor(2f);

Note how I changed the class name to uppercase - you should do that too. Having lowercase class names will have you run into issues sooner or later.

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

197 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

Related Questions

My character can move with WASD, but it slides and shows no animations 1 Answer

How to make sure than one script affect the player of the specific client, and not the other 2 Answers

How do I keep my character facing the direction of travel after movement stops? 1 Answer

cannot drag script to player.Guitext error,cannot drag player script to the player in hierarchy 2 Answers

Character rotation and move to mouse click point with speed 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