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 easy · Jul 02, 2011 at 09:33 AM · charactercontrollergradually

character controller

Hi, I'm trying to figure out how can i move my character to some fixed height,for example if my character position is at 10 and i want to lift him in gradually to let say 15. this is what i'm doing:

 void Update()
 {
   float nextHeight =  Mathf.Lerp( transform.position.y, transform.position.y + 5, 3f );
   MoveVector.y = nextHeight;
   myCharacter.Move( MoveVector );
 
 }

but my character is infinity flying up.

help please.

Comment
Add comment · Show 2
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 Grady · Jul 02, 2011 at 09:37 AM 1
Share

can you please edit and then highlight your code, and then click the little button with all the ones and zeros so we can read your code better :D

-Grady

avatar image aldonaletto · Jul 02, 2011 at 11:33 AM 0
Share

Are you using the standard Unity's FPS Controller?

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by AngryOldMan · Jul 02, 2011 at 12:20 PM

There are a few ways to do this. You could turn it into a coroutine in order to give you a large amount of control over how fast it moves, by what amount and when it stops. A simpler or faster way though would be to put an if statement in to stop the code for the character rising. Something like:

 void update()
 {
    if (characterHeight <= maxHeight)
    {
       MoveVector.y = nextHeight;
    }
 }
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
1

Answer by aldonaletto · Jul 02, 2011 at 02:16 PM

If you're using the standard FPS Controller, you can make a "jet pack" effect by setting a vertical velocity. In this script, when you press F the character starts to get height like moved by a jet pack; you can't control directions fully while it's going up, but as soon as you release the F key the control keys work as usual:

 var cMotor: CharacterMotor;
 
 function Start(){
 
     cMotor = GetComponent(CharacterMotor);
 }
 
 function FixedUpdate(){
 
     if (Input.GetKey("f")){
         cMotor.SetVelocity(Vector3.up*1.5); // vert velocity = 1.5
     }
 }
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
-1

Answer by easy · Jul 02, 2011 at 10:42 AM

Hi again,

this is the code now:

 float maxHeight;
 
     void Start()
     {
       maxHeight = transform.position.y + 5f;
     }
     void Update()
     {
       float nextHeight =  Mathf.Lerp( transform.position.y, maxHeight , 3f );
       MoveVector.y = nextHeight;
       myCharacter.Move( MoveVector );
     
     }

still the same problem. help please.

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 save · Jul 02, 2011 at 10:19 AM

It's because you position towards the current transforms position +5 every frame. You have to store the Y-position first and transform against that instead.

 var nextHeight : float = 10.0;
 
 function setNextHeight (object : Transform) {
     nextHeight = object.transform.position.y + 5;
 }

Call the function on a given event (could be a trigger) with:

 setNextHeight(theObject);

Then transform towards nextHeight:

 Mathf.Lerp( transform.position.y, nextHeight, 3f );

(Pseudo-mix of js)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Stopping the Character Controller 0 Answers

Detecting collisions with OnControllerColliderHit when not moving 0 Answers

Conflict between CharacterController and NavMeshAgent 1 Answer

Lerp can someone explain please 2 Answers

OnCollisionEnter does not work 4 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