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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by IKilledKenny_2 · Jan 29, 2015 at 10:40 PM · javascriptmovementplayertimefollow

How to Follow By Getting KeyDown?

Hello Unity3D.I have a question about how to following?How i can make it that whenever i Get a key down i can follow a object and after a certain amount of time the player stops following that object?For example.When i GetKeyDown"b" my character follows this object.Then,when it has been atleast 5 seconds or over 5 seconds the character stops following the object.Then when i GetKeyDown"b" again it follows the object from the new position that it was stopped at before.If anyone knows how i can do this.Can you please tell me how?

P.S Heres a script if anyone still doesn't understand

 var target : Transform;
 var moveSpeed = 20;
 var rotationSpeed = 0;
 var myTransform : Transform;
 
 function Start() {
  
         target = GameObject.FindWithTag("Dummy").transform;
 }
  
 function LateUpdate () {
 
 
     if(Input.GetKeyDown("1"))
        myTransform = transform;
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }
Comment
Add comment · Show 3
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 Starwalker · Jan 29, 2015 at 10:59 PM 1
Share

The sequence of events is wrong here, LateUpdate happens after Update, hence you want to register the $$anonymous$$eyPress in Update, Count to 5 using Couroutines (during these 5 secs, the LateUpdate's code does its thing) and then comes out.

Pseudo Code:

stopFollowing = false;

Update{

  1. Register $$anonymous$$eyDown

  2. Start couroutine, yield at 5 secs

  3. stopFollowing = true

}

LateUpdate {

if(!stopFollowing) { myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward moveSpeed Time.deltaTime;

}

Couroutine {

yield (5); }

}

avatar image Simon-Larsen · Jan 29, 2015 at 11:12 PM 0
Share

Ins$$anonymous$$d of coroutines you could also just set a timestamp as you press a key. if (Time.time < timestamp + followTime) $$anonymous$$ove();

avatar image IKilledKenny_2 · Jan 30, 2015 at 05:15 AM 0
Share

Im Sorry...But do you mean this?Im trying to figure out what you mean by timestamp

 var target : Transform;
     var moveSpeed = 20;
     var rotationSpeed = 0;
     var myTransform : Transform;
     var timeBetweenShots = 10.0;
     private var nextFire : float =0.0;
     private var timestamp = -10.0;
     private var followTime : float =0.0;
     
     function Start() {
      
             target = GameObject.FindWithTag("Dummy").transform;
     }
      
     function Update () {
     
     if (Input.Get$$anonymous$$eyDown("1")&& Time.time < timestamp + followTime) {
              if (Time.time > timestamp + timeBetweenShots ) {
                   Debug.Log("I'm firing now");
                   timestamp = Time.time;
                   
                   
             }
         }   
     }    
     
      
      
      
     function LateUpdate () {
     
            myTransform = transform;
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
         Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }`

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Camera rotation around player while following. 6 Answers

Player Going through colliders 1 Answer

Enemy following Player in range 2 Answers

Which is better for player movement: physics or translation 1 Answer

Endless runner track script 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