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 erap129 · Nov 22, 2017 at 09:12 AM · lerpheightflyingparameterhovering

make character hover in correlation with parameter

Hello, In the game I am creating I am trying to make my player hover at varying heights. These heights will be chosen by an outside parameter (in my case a static variable that I get from a TCP connection). I'm really having trouble doing this for some reason Attached here is a video demonstrating my problem: (yes, I am working on the survival shooter project from Unity): https://youtu.be/0DtiSQrCzuk

The player is supposed to collect power-ups (like the flying powerup here) and the number on the left-top corner of the screen is supposed to control his hovering. I didn't want the movement between different heights to be jerky so I tried to manage it through my code (not very succesfully). What happens is instead of hovering at a constant height in relation to the parameter - the character just "jumps" up and down in a weird fashion. If I set the drag to infinity the character just hovers up infinitely (continues to go up). attached below also is the script which controls the flying (this script isn't attached to the player - but to an empty gameobject in the scene - that's why I have to search for the player gameobject in the script) Any help would be appreciated! I thought maybe to deal with the hovering using forces (hovering at a constant height isn't the biggest issue for me - what's important is that the hovering will be closely correlated to the incoming parameter from the TCP connection) Thank you, Elad

Code of flying script:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class FlyScript : PowerUpScript {
 
     public GameObject Clouds;
 
     // Update is called once per frame
     
     void FixedUpdate () {
         if (player == null) // in case the powerup loaded before the player
             player = GameObject.FindGameObjectWithTag("Player");
         if (powerUpText == null) // in case the powerup loaded before the text
             powerUpText = GameObject.Find("HUDCanvas/PowerUpText").GetComponent<UnityEngine.UI.Text>();
 
         if (active) // do work only if in active state
         {
             if (time < timeLimit)
             {
                 powerUpText.text = "Flying: " + (timeLimit - time).ToString("0.00"); // show how much time left for powerup
                 time += Time.deltaTime;
                 if (player.transform.position.y  < TCPInput.param + 0.1f)
                     player.transform.position = new Vector3(player.transform.position.x, player.transform.position.y + 0.05f, player.transform.position.z);
                 else if (player.transform.position.y - 0.1f > TCPInput.param)
                     player.transform.position = new Vector3(player.transform.position.x, player.transform.position.y - 0.05f, player.transform.position.z);
                 Clouds.transform.position = player.transform.position; 
             }
             else
             {
                 if (powerUpText.text != "")
                     powerUpText.text = "";
                 if (player.transform.position.y > 0)
                     player.transform.position = new Vector3(player.transform.position.x, player.transform.position.y - 0.05f, player.transform.position.z);
                 else
                     Deactivate();
             }  
         }
     }
     
 
     public override void Deactivate()
     {
         active = false;
         player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY; // freeze position of player when not in fly mode
         Clouds.SetActive(false);
     }
 
     public override void Activate()
     {
         Clouds.SetActive(true);
         active = true;
         time = 0;
         player.GetComponent<Rigidbody>().constraints = 0;
     }
 }
 


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gamerkaa · Nov 22, 2017 at 09:52 AM

Add an animation controller to your GameObject. Tick Apply Root Motion. Create an animation for jump. Add states in animation controller for idle and jump. Set the jump animation. Add a parameter jump in animation controller. Create a script in the GameObject. In the Update() {} set the jump parameter to Input.GetKey(KeyCode.LeftShift).

Run the program in the Editor and hit LeftShift key.

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

73 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Increasing Controller Height Causes FPSInputController To Fly 0 Answers

Broomstick Flying with Rigidbody? 1 Answer

How to move an object from point A to point B with one key press 3 Answers

Grid Movement - Lerp that ends with precise values 6 Answers

Lerping textures 3 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