- Home /
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;
}
}
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.