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 Aladine · Feb 01, 2014 at 08:49 AM · 2drotationlerpfollow

Lerp toward object while keeping a constant speed

Hello everyone,

Am trying to make a "following effect", something like homing missles, i tried to use both Slerp and Lerp but am always facing the same problem which is the acceleration loss at some point, i want to know if there is a way to follow an object while keeping the same speed. Here is the complete class that i use :

 using UnityEngine;
 using System.Collections;
 
 public class EnemyFollow : MonoBehaviour
 {
         public float speed ;
         public float damping;
         Transform myTrans;
         public Transform target ;
         // Use this for initialization
         void Start ()
         {
                 myTrans = transform;
         }
 
         // Update is called once per frame
         void Update ()
         {
                 Quaternion rotation = Quaternion.LookRotation (target.position - myTrans.position);
                 myTrans.rotation = Quaternion.Lerp (myTrans.rotation, rotation, Time.deltaTime * damping);
                 myTrans.position = Vector3.Lerp (myTrans.position, target.position, Time.deltaTime * speed);
         }
 }

And here is a web version of the prototype so you can better understand what am saying, use arrows to control your spaceship : Prototype-test

PS : this is an off topic but do you have any idea why the enemy "flip" around sometimes ?

thank you and have a great day

Comment
Add comment · Show 9
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 robertbu · Feb 01, 2014 at 09:10 AM 0
Share

Change your Vector3.Lerp() to Vector3.$$anonymous$$oveTowards() on line 21. The parameters remain the same. You will have to adjust your 'speed' variable since it will behave differently in $$anonymous$$oveTowards(). It will become units per second.

avatar image Aladine robertbu · Feb 01, 2014 at 09:26 AM 0
Share

still don't have the effect am looking for, i don't thing moveTowards or lerp/slerp are the correct calls, since i don't need the object to move toward the player, i need it to always go forward based on his rotation, i don't know if you can see what am trying to do here or not, but the goal is to have an enemy who follow the player in a "slippery way" so the player can dodge and make the enemy go into obstacles and die

avatar image KellyThomas robertbu · Feb 01, 2014 at 09:51 AM 0
Share

@Aladine then look at `Quaternion.RotateTowards()`.

avatar image Aladine robertbu · Feb 01, 2014 at 11:08 AM 0
Share

@$$anonymous$$elly$$anonymous$$ i already tried that but the Quaternion type is somehow "over complicated" when it's used in a 2D game, however, RotateTowards() almost do exactly the same thing that my previous code do

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by KellyThomas · Feb 01, 2014 at 12:29 PM

This should rotate towards the target using only z-axis rotations.

     float angleSpeed = 180f;
     float movementSpeed = 10.0f;
     float nearEnough = 20f;
 
     void RotateTowards(Vector3 targetPosition) {
         Vector2 targetDirection = targetPosition - transform.position;
         float idealAngle = Mathf.Rad2Deg * Mathf.Atan2(targetDirection.y, targetDirection.x);
         float currentAngle = transform.rotation.eulerAngles.z;
 
         if(Mathf.Abs(Mathf.DeltaAngle(idealAngle, currentAngle)) > nearEnough) {
             float nextAngle = Mathf.MoveTowardsAngle(currentAngle, idealAngle, angleSpeed * Time.deltaTime);
             transform.rotation = Quaternion.Euler(new Vector3(0.0f, 0.0f, nextAngle));
         }
     }
Comment
Add comment · Show 7 · 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 Aladine · Feb 01, 2014 at 12:35 PM 0
Share

at line 5, the "target" is a transform object, should i replace it by target.rotation.eulerAngles.z

cause $$anonymous$$athf.$$anonymous$$oveTowardsAngle take 3 floats, PS : i did the replacement and the enemy go crazy xD EDIT i like the way your code works, am starting to think about making a raycast that stop the rotation in case the enemy is facing the player cause for now all that it rest is stopping the rotation when it is in the right direction

avatar image KellyThomas · Feb 01, 2014 at 12:48 PM 0
Share

Yeah... sorry line 5 should have been idealAngle i.e. the angle towards the target.

If you want to throttle the enemy you could reduce angleSpeed or do a check with $$anonymous$$athf.DeltaAngle(myTrans.rotation.eulerAngles.z,idealAngle) and stop rotating when "near enough is good enough".

avatar image Aladine · Feb 01, 2014 at 12:49 PM 0
Share

this make it pre-perfect : float angle = 10; if (Vector3.Angle (myTrans.right, myTrans.position - target.position) > angle) { /your code here/ } but did you notice the "always-rotate-left" thing ??? anyway to fix that ??

avatar image KellyThomas · Feb 01, 2014 at 01:07 PM 0
Share

Vector3.Angle() will return both positive and negative values, you might want to wrap it in a call to $$anonymous$$athf.Abs() to remove the bias in the condition posted above.

avatar image Aladine · Feb 01, 2014 at 01:45 PM 0
Share

i don't see any need for that, but i tested it anyway, and the result is the same, i think what i need here is a way to tell either the target (player) is on the left or on the right of the enemy, and by left and right i don't mean their X position in the world, but on which side of the enemy the player is, do you get what i meant ?

Show more comments
avatar image
0

Answer by robertbu · Feb 01, 2014 at 02:22 PM

Your question asked and your follow-up comments seem very different. Assuming your original code works except for flipping, I have to assume that 1) you are rotating around the 'z' axis for this 2D game and 2) the 'forward' of your follower is what is pointed at the the thing it is following. If so, you can solve your flip by adding the optional second parameter to the LookRotation() call. It is the 'up' vector, and for a 2D game planed on the XZ palne you want to use Vector3.forward:

  Quaternion rotation = Quaternion.LookRotation (target.position - myTrans.position, Vector3.forward);
  myTrans.rotation = Quaternion.Slerp (myTrans.rotation, rotation, Time.deltaTime * damping);

Note the use of .Slerp() here, will produce an eased rotation. You may want to use RotateTowards() instead. If so, you will need to increase the value of 'damping'.

Given you want, "i need it to always go forward based on his rotation," you want to change line 21 to:

  myTrans.position += myTrans.forward * Time.deltaTime * speed;

Again this assumes you are using a 3D object and want the 'forward' side (the side facing positive 'z' when the rotation is (0,0,0)) to be the side aimed by the rotation.

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 jake4040 · Feb 05, 2015 at 03:14 AM

How about applying a torque to the homing object with a magnitude based on the difference in angle between the direction the missile is pointing and the ray from the homing object to the player? Then you can use the inertia of the homing object and/or the magnitude of the applied torque to control how tightly it can track.

You would also need to continue to apply forward thrust to the object so it will curve based on it's new direction.

This would use the physics engine to basically model a "homing missile" type effect.

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

20 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

Related Questions

[Help] Object flips around when using transform.Rotate 3 Answers

Rotate Object to face player 2d 1 Answer

How to use one animation and Animator Controller on multiple objects? [2D] 0 Answers

Uniform rotation - uniform lerp 1 Answer

Camera following/looking at aircraft 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