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 Vadom6 · Mar 02, 2021 at 03:23 AM · scripting problemshootingprojectilespeed issues

Variable Projectile speed that changes based on distance from Target

I have an Issue where I want to change a Projectiles speed based on its distance from the target. I want it to be faster the further away from the target it is and slower the closer it gets to the target.

This is the code I am using now :

     public int damage;
     public AnimationCurve speedCurve;
     public float minSpeed, maxSpeed, speed;
     public LayerMask enemyLayer;
     public GameObject Target;
     
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     { 
         transform.position = Vector3.MoveTowards(this.transform.position, Target.transform.position, speed);
         transform.Rotate(0.0f, 0.0f, 1f);
     }

I have tried using animation curves along with other methods to solve this issue, but so far haven't had any success.

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 pauldarius98 · Mar 02, 2021 at 07:50 AM

Ok, so if you want to use an AnimationCurve to model your projectile speed, make sure that the interval is normalized [0,1] and then set the speed that you desire for that interval (IE: minSpeed could be at 0, maxSpeed at 1 or even at 0.7, however you desire). Then, we need to get the distance percentage from target (1 - we are at max distance, 0 - we hit the target) To do this we first calculate the initial distance and then in each frame we calculate the distance till we hit the target and we divide it by the initial distance, in orther to obtain that normalized percentage. The code is bellow

      public int damage;
      public AnimationCurve speedCurve;
      public float minSpeed, maxSpeed, speed;
      public LayerMask enemyLayer;
      public GameObject Target;
      
      private float totalDistance;
      
      // Start is called before the first frame update
      void Start()
      {
          totalDistance = (transform.position - Target.transform.position).magnitude;
      }
  
      // Update is called once per frame
      void Update()
      { 
          var remainingDistance = (transform.position - Target.transform.position).magnitude;
          speed = speedCurve.Evaluate(Math.Clamp01(remainingDistance / totalDistance))
          transform.position = Vector3.MoveTowards(this.transform.position, Target.transform.position, speed);
          transform.Rotate(0.0f, 0.0f, 1f);
      }
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 MUG806 · Mar 02, 2021 at 07:56 AM

You could try something along these lines:

Math.Lerp(minSpeed, maxSpeed, distanceToTarget/maxDistance)

Lerp will pick a value between the first two arguments based on how big the third argument is. If the third argument is 0 you get minSpeed, if it's 1 you get maxSpeed. So if you pass in the current distance divided by the distance at which you want max speed, you should get the desired behaviour.

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

210 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

Related Questions

My Projectile System has Another Bug 1 Answer

Bullet only firing in 1 direction. 1 Answer

Can someone tell me how to make a projectile shoot at a Transform without moving to the Transform's new position? 1 Answer

Shooting in 2 dimensional games 2 Answers

Gun Fire, sparks on Collision 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