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
1
Question by $$anonymous$$ · Oct 09, 2012 at 11:40 AM · teleport

Force Teleport

Hey Everyone here is a script im working on to add a force to a teleport where you can control how far you go the longer you hold down the right arrow key. I cant seem to find the problem, any advice or help to point outs whats wrong would be greatly appreciated. :)

 var character : GameObject;
 var target   : GameObject;  // Target to shoot away.
 var timer    : float = 1;  // 10 means it takes 10 seconds for full force.
 var minForce : float = 10; // Minimum force applied on shot.
 var maxForce : float = 50; // Maximum force applied on shot.
 private var power    : float = 0;  // Power charge.
 
 function Play() {    
     power = 0;
     while (!Input.GetKeyDown("right")) {           yield; } // Wait until press.
     while ( Input.GetKeyDown("right")) { Charge(); yield; } // Charge until release.
     Shoot();
 }
 
 function Charge() {
     power = Mathf.Clamp01(power + (1 / timer) * Time.deltaTime);
 }
 
 function Shoot() {
     var impulse = Mathf.Lerp(minForce, maxForce, power);
     target.AddForce(transform.forward * impulse, ForceMode.Impulse);
 }
 
 function Update () {
 if (Input.GetKeyDown ("right"))
 {
 transform.Translate(character.transform.right.normalized *-5.0f);
 
 }
 
 if (Input.GetKeyDown ("left"))
 {
 transform.Translate(character.transform.right.normalized *5.0f);
 
 }
 
 if (Input.GetKeyDown ("up"))
 {
 transform.Translate(character.transform.up.normalized *5.0f);
 
 }
 
 if (Input.GetKeyDown ("down"))
 {
 transform.Translate(character.transform.up.normalized *-5.0f);
 }
 
 }

Comment
Add comment · Show 1
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 hvilela · Oct 09, 2012 at 11:46 AM 0
Share

Please, format your code. (Button 101010)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Oct 09, 2012 at 12:15 PM

There are some weird/wrong things in your script. First: you should use Input.GetKey instead of Input.GetKeyDown in the Play coroutine - GetKeyDown returns true only during the frame the key was pressed. Second: Play isn't being called anywhere in the script you've posted. Third: target is a GameObject, but you can only AddForce to a Rigidbody. If you're assigning to target in the Inspector, just declare it as a Rigidbody and keep your original Shoot code. If target may be assigned at runtime, on the other hand, check whether the target object has a rigidbody, and if so, apply the force to it - like this:

 function Shoot() {
   if (target.rigidbody){
     var impulse = Mathf.Lerp(minForce, maxForce, power);
     target.rigidbody.AddForce(transform.forward * impulse, ForceMode.Impulse);
   }
 }

NOTE: Make sure that Play isn't called again while it's executing! You can use a boolean flag: set it to true when Play starts and to false at the end, and abort Play calls when the flag is true:

 var shooting = false; // boolean flag
 
 function Play(){
   shooting = true;
   ...
   Shoot();
   shooting = false;
 }
 
 // in the caller code:
   ...
   if (Input.GetGeyDown(...) && !shooting) Play();
   ...
 

 
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 plof27 · Jun 12, 2018 at 09:51 PM

I know I'm 6 years late on this, but I came across this post because I ran into the same problem. There was a thread here which actually had a functional solution. You need to uncheck Apply Root Motion on the object's Animator component. For some reason, this causes weird locking on some, but not all, axes even if there is no parent object in the hierarchy. Hopefully this helps any other lost souls who find themselves here years in the future.

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

10 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

Related Questions

Spawning System 1 Answer

Touch teleport script isnt working 2 Answers

Mirror a Vector3 1 Answer

Velocity not resetting to zero? 1 Answer

Camera Following character that teleported 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