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 FDMpro · Oct 20, 2018 at 03:46 PM · rigidbodytransformlookattarget

Intercepting Alghorithm not wok properly

Try to Intercept Object A with Object B, when Calculating Arrive-time and Probable Location in Object A I show something to Know calculation data. But calculate position is not work properly best to see the Gif Below (Green One is object A and Red in Object B, the Grey is Calculated position that object A should look and shoot) (As see the Gray Object is Not go Smoothly it's get back to Red Position repeatedly):
Trulli
This is the Code for Intercept:

 public GameObject Enemy;
     public GameObject GhostObject;//Grey Object That Show Calculating Position
 
     Rigidbody enemyRig;
     Transform enemyPos;
 
     Vector3 bulletSpeed = Vector3.forward * 0.08f;
 
     Vector3 current;
     Vector3 last;
 
     float time;
 
     Vector3 enemyVel;
 
     // Use this for initialization
     void Start ()
     {
         enemyPos = Enemy.transform;
         enemyRig = Enemy.GetComponent<Rigidbody>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         enemyVel = CalEnemyVel();
 
         Vector3 targetPos = enemyPos.position;
 
         Vector3 Vr = enemyVel - transform.forward * 10f;
         Vector3 Sr = targetPos - transform.position;
 
         time = Sr.magnitude / Vr.magnitude;
 
         Vector3 lookpos = targetPos + (enemyVel * time);
     
         transform.LookAt(lookpos);
 
         GhostObject.transform.position = lookpos;
     }
 
     Vector3 CalEnemyVel()
     {
         Vector3 vel;
 
         current = enemyPos.position;
 
         if (last != null)
         {
             vel = ((current - last) / Time.deltaTime);
         }
 
         else vel = Vector3.zero;
 
         last = current;
 
         return vel;
     }


Movement Method for Red

        public float speed = 0.5f;
     
     void Update ()
     {
         float h = Input.GetAxis("Horizontal");
 
         //GetComponent<Rigidbody>().MovePosition(transform.position + Vector3.left * h * speed);
 
           transform.Translate(Vector3.left * h * speed);
     }



Comment
Add comment · Show 4
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 hexagonius · Oct 20, 2018 at 09:30 PM 0
Share

I would say that there are frames where in CalEnemyVel last and current are the same value and turn zero. I don't know how you move, but most likely not as often as this is called

avatar image FDMpro hexagonius · Oct 21, 2018 at 02:29 AM 0
Share

I try Two method first move with rigidbody.$$anonymous$$ovePosition and then transform.translate. getting velocity directly from rigid body cause weird result so I calculate velocity myself. now with both this thing happens. I adding movement method.

avatar image TreyH · Oct 21, 2018 at 02:31 AM 0
Share

This sounds like a class assignment?

avatar image FDMpro TreyH · Oct 21, 2018 at 02:38 AM 0
Share

I try to make Tower Defense Game.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FoodLover195 · Oct 21, 2018 at 03:24 AM

Hi @FDMpro
Although I didn't read through most of your code (not very clean to read), from my understanding the green object has to shoot infront of the red object because you have calculated the interception point using the bullet speed, enemy speed, and enemy direction. If that's the case are you sure it's not working? From the gif you attached it would seem that the ghost object was calculating correctly. Assuming I'm not missing anything, the results you have would make sense because as the red character turns, it's velocity or rather Input.GetAxis slows down. This obviously means the red object isn't going to travel as far, therefore the ghost retracts slightly. This continues until eventually the force isn't cancelling itself and it starts moving in the opposite direction. I hope that makes sense, just tag me if you have anymore questions ^_^

Comment
Add comment · Show 5 · 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 FDMpro · Oct 21, 2018 at 04:04 AM 0
Share

Thanks for reply. at first sorry about my bad English . as you say the green object should look And shoot at red object. but the bullet is not ray-cast (here I don't show bullet) it's have a speed and I have to calculate ai$$anonymous$$g position for bullet to hit object. the grey object position show where calculated result is pointing. the problem comes now is if you look at gif the gray object in one frame is at calculated position and in some frame is in red position (when red is moving) this most not to happen because the red object is moving continuously . as @hexagonius say, maybe in some point velocity is become zero or something is become zero so grey not go to desire location but I can't find it.

avatar image FoodLover195 · Oct 21, 2018 at 04:46 AM 0
Share

Yes exactly. It's the Input.GetAxis function. Sorry I tried to explain it but apparently not well enough. You can trust me when I say the code is working 100% (At least seems so from my understanding of the movement). The issue is your desired outcome may not align with the actual outcome. I'l try and show whats going on:

Controls relative to the h variable which is using Input.GetAxis
Press Right - h goes up
Press Left - h goes down

There for imagine this:
Player holds down right. h goes from 0 to 1
Then player holds down left. h goes from 1 to -1

If you relate that back to your ghost object, when h is 1, the object is 1 unit infront of your enemy. Then as you click left to make h -1, it needs to pass back through the enemy (at h = 0) to get to the left side when h is -1.

Does that make sense?

avatar image FDMpro FoodLover195 · Oct 21, 2018 at 05:52 AM 0
Share

O$$anonymous$$ I update the Gif so maybe shows it better and Yes your right the way of axis working is through 0 to 1 but if you look the stuttering is happens even when I try to go same direction for a long time. And other thing if the Axis go through 0 to 1 it's not have to go back it's go slowly until reach full speed but now it's go back and forward while Red Just move in constant speed(h = 1).

avatar image FoodLover195 FDMpro · Oct 21, 2018 at 06:29 AM 0
Share

Oh, okay. I didn't think the jittering was such a big deal provided it stays within certain bounds, but I can see how that would be an issue. I've tried to analyse your code but somethings don't make sense to me.

Firstly, I noticed you are using a variable called enemyPos in CalEnemyVel() however you only set the enemy pos in the start function? If so wouldn't this affect the calculations of a moving enemy if you use a stationary vector? Also is the purpose of vr and sr?

At this point I'm also just gonna build my own little test project and see if I can come up with a solution :)

Show more comments

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

143 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

Related Questions

how do i make this lag less? 1 Answer

Resetting rigidbodies' transform values 1 Answer

Animation or smooth teleportation 0 Answers

How can I add Rigidbody to Transform? [.js] 1 Answer

Change size of rigidbody programmatically 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