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 JakeLeB · Apr 19, 2013 at 12:13 AM · characterjumphover

Hovering Cube on CapsLock?

I've coded a basic movement system that lets a cube walk around, sprint and jump. I'd like to have it though so if left alt is on then the cube goes into a hover state and is able to "fly" around with the WSAD moving the character and space increasing the Y and ctrl decreasing the Y.

I'm not asking that someone writes the code for me but if you could point me in the right direction that would be fantastic. I'm quite new to the character coding and I've been doing a lot of the tutorials but I can't seem to figure this out.

I basically just want it so that when lAlt is true the cube slowly ascends and stays at a set Y position and then when lAlt is false it descends back down.

My code is:

  // Player Variables
 var speed = 7.0; 
 var rotateSpeed = 3.0;
 
 //Jumping Variables
 var moveDirection : Vector3 = Vector3.zero;
 var gravity = 0.3;
 var jumpHeight = 15;
 
 var lAlt = false;
 
 function Update () 
 {
 //Basic Movement
     var controller : CharacterController = GetComponent(CharacterController); 
     var forward = transform.TransformDirection(Vector3.forward); 
     var curSpeed = speed * Input.GetAxis ("Vertical"); 
     transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0); 
     controller.SimpleMove(forward * curSpeed);
 
 //Left Shift to boost speed by 9
     if(Input.GetKey(KeyCode.LeftShift))
         {
             speed = 15;
         }
     else
         {
             speed = 6;
         }
         
         
     //Jumping input
     if(Input.GetKey(KeyCode.Space))
     {
     moveDirection.y = jumpHeight;
     }
     
     moveDirection.y -= gravity;
     controller.Move(moveDirection * Time.deltaTime);        
 
     if(Input.GetKey(KeyCode.LeftAlt))
     {
         if(lAlt == true)
         {
         //HoverCode
         lAlt = false;
         }
         else
         {
         //Stop Hovering
         lAlt = true;
         }
     }
 
 //End of Update()
 }
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 iwaldrop · Apr 19, 2013 at 05:41 AM

If you're using a rigidbody just turn off the gravity. If you want the floaty effect when you first engage flight mode, then just sample the current y position and add a steadily decreasing value to it over the next second or so.

If you want to move up 1m over 2 seconds, then:

 IEnumerator FloatEffect(float time, float distance)
 {    
     rigidbody.useGravity = false;
     
     float startTime = Time.time;
     Vector3 startPosition = transform.position;
     Vector3 endPosition = startPosition + (Vector3.up * distance);

     while (startTime + time >= Time.time)
     {
         transform.position = Vector3.MoveTowards(transform.position, endPosition, distance * Time.deltaTime);
         yield return null;
     }
 }

I'll leave it up to you to figure out how to smooth it. ;)

Comment
Add comment · Show 4 · 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 Konkor4 · Apr 19, 2013 at 06:24 AM 0
Share

Sorry for the late reply, Unity Answers said they had removed this question. I've tried implenting the code you posted into $$anonymous$$e but I'm getting errors about missing semicolons when they are there.

avatar image iwaldrop · Apr 19, 2013 at 08:38 PM 0
Share

Check the rest of the code. I ran this snippet fine.

avatar image Konkor4 · Apr 20, 2013 at 04:33 AM 0
Share

$$anonymous$$y code is fine, the errors are with the code you posted. Unity says that it expects ; but it isn't there even though there is and the same with ). I've gone to each line and replaced them, tried adding extras too but still errors.

avatar image iwaldrop · Apr 20, 2013 at 04:39 AM 0
Share

Well, if you double-click the log entry it should take you to the line that it's complaining about. I just manufactured a similar error and this is what it says:

Assets/Scripts/Generic/StrobeObject.cs(26,17): error CS1525: Unexpected symbol }', expecting ;'

That means that the compiler expects a semi-colon at Line 26, position 17. What does your error message say? Can you post it?

avatar image
0

Answer by s_guy · Apr 20, 2013 at 05:20 AM

For smoothing actor movement, as with your intent to smooth hover, search for Lerp and SmoothBlend methods and investigate them. Search on here seems to suck (fail or never conclude), so use Google and search for things like "unity lerp" or "unity lerp movement". Most of the relevant stuff will point back to this site. You might want to figure out these techniques in isolated studies before trying to apply them to your need.

You probably want a general gravity solution to handle your descent or hover turning off, and the object returning to ground. Search for "unity gravity character controller".

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

13 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

Related Questions

Minor Addition to Jumping using CharaterController 1 Answer

Why can't my character jump 2 Answers

Using a touch event for character motor 0 Answers

Let Character Controller jump. 1 Answer

Jump Script ow to make it??? 2 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