How do I give the player free flight?
What I would like to do is give my player the option of free flight.
So for example, if I were to press the space bar twice(once to jump and twice in the air to initiate flight) the player model would fly.
To go higher the player would press and hold the left mouse button and to go lower they would hold the right mouse button.
I'm making a test game and I would really like to implement this feature, I'm using C# script and would appreciate any help or advice on the topic :)
I've made the model, I'm currently working on it's animations so I'm hoping by the time I'm done that someone would have posted an answer to help with this. If it helps, I'm using a 3D game, not 2D. If someone could post an example script, I would greatly appreciate it.
I'm pretty new to Unity and I still consider myself pretty inexperienced so if there's any special options or such, providing screenshots would be an incredible bonus.
I forgot to add that I'm using the a basic script that I learned from watching a tutorial:
using UnityEngine;
using System.Collections;
public class PlayerGame : MonoBehaviour
{
private Animator anim;
// Use this for initialization
void Start()
{
anim = gameObject.GetComponentInChildren<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("w"))
{
anim.SetInteger("AnimPar", 1);
}
else
{
anim.SetInteger("AnimPar", 0);
}
if (Input.GetKeyDown("space"))
{
anim.SetInteger("Jump", 2);
}
else
{
anim.SetInteger("Jump", 0);
}
if (Input.GetKey("left shift"))
{
anim.SetInteger("Run", 3);
}
else
{
anim.SetInteger("Run", 0);
}
}
}
I think I have to edit something along the Y axis but again I'm very new to this and I don't know exactly how to do this. I've tried looking up tutorials for this but haven't had much luck as they don't usually work and just give me lots of errors, despite mimicking the instructor's guide down to the letter.
Don't forget to format your pasted code with the 101010 button.
Here's a handy shorthand conditional that might save you some typing:
int animpar = Input.Get$$anonymous$$ey("w") ? 1 : 0;
anim.SetInteger( "AnimPar", animpar );
Based on what you stated above, it shouldn't be too hard to get what you're after. You need to trigger a special flight state, during which your input does things differently than in your normal walking state.
You've decided your method for triggering this state already. You'll also need a way to stop flying, whether it's a button press or colliding with the ground or other objects. This state could be as simple as a bool called isFlying.
$$anonymous$$ost games that involve jumping also track a bool called isGrounded. This is deter$$anonymous$$ed by firing a ray or based on collision with the ground. Lots of info about this stuff is available.
So, if you're not currently grounded, and you press the jump button, isFlying becomes true. When isFlying is true, the inputs you mentioned trigger a change in the player's Y position. Easy as pie. You seem to have a handle on it already, so give it a shot and report your progress if you get stuck.
As with all things program$$anonymous$$g, your mileage may vary. Based on exactly what is written above, this is the simplest answer, but not necessarily the best or most through.
Would you be able to provide some example code? I tried the following but my character still isn't able to fly:
using UnityEngine; using System.Collections;
public class $$anonymous$$alusBeta : $$anonymous$$onoBehaviour { Animator anim;
public float speed = 10.0f;
public float rotationSpeed = 100.0f;
void Start ()
{
anim = this.GetComponent<Animator> ();
}
void Update ()
{
float translation = Input.GetAxis ("Vertical") * speed;
float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
if (Input.Get$$anonymous$$ey ("Space"))
{
anim.SetBool ("isFlying", true);
transform.Translate (0, 0.5f, 0);
}
else
{
anim.SetBool ("isFlying", false);
}
transform.Translate (0, -0.2f, 0);
}
}
Again I greatly appreciate the help on this matter :)
Hey again! Look for the 101010 button when composing a post here at UA. It's up there with all the text formatting buttons. You'll see it next time. Highlight all of your pasted code (only code) and click it to ensure good formatting.
It's still wholly unclear why you're getting unexpected results. It's early enough in your learning career that you should never be afraid to scrap big chunks of work and start over. Questions like this (writing back and forth several times but we're still both confused) are generally beyond the scope of UA volunteer work.
You appear to be making a character controller from scratch. Whether or not that is a good idea depends on what you hope to achieve. Writing a high-quality controller is not a beginner's task, which is why they give you some examples to build from.
It might be in your best interest to start over with the example controller setup from Standard Assets and build onto it. Google for a C# version if you discover it's written in JS. You don't wanna mess around with two languages, and C# is superior.
It wouldn't be too hard to add a line or two to that controller's jump logic that would allow you to "jump forever" - which is what you were essentially trying to do in the script above. Once you've got that in place, you can customize it further to get the results you actually want, hook up separate animations, unique flight controls, all that jazz.
If the controller from Standard Assets is not similar enough to your desired end result to be worth using - and you're still stuck on this problem - I'd still recommend starting over fresh. Sorry I couldn't help you deter$$anonymous$$e the issue this time, but remember, scrapped work isn't wasted effort if you learned something, and sometimes it's better to scrap it than fight it.
Answer by CraftyMaelyss · Mar 29, 2017 at 03:50 AM
@AlwaysSunny I tried a new code(which I think is better) but I'm still running into the same problem of not being able to fly.
using UnityEngine;
using System.Collections;
public class KalusBeta : MonoBehaviour
{
private CharacterController controller;
private float verticalVelocity;
private float gravity = 14.0f;
private float jumpForce = 10.0f;
private void Start()
{
controller = GetComponent<CharacterController>();
}
private void Update()
{
if (controller.isGrounded)
{
verticalVelocity = -gravity * Time.deltaTime;
if (Input.GetKeyDown (KeyCode.Space))
{
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
Vector3 moveVector = Vector3.zero;
moveVector.x = Input.GetAxis("Vertical") * +40.0f;
moveVector.y = verticalVelocity;
moveVector.z = Input.GetAxis("Horizontal") * -40.0f;
controller.Move (moveVector * Time.deltaTime);
if(Input.GetKey("Space"))
{
transform.Translate(0,+0.6f,0);
}
}
}
(Also thank you so much for pointing out the 101010 button, I feel like a dork for not noticing it earlier XD)
I tried adding + to the Y translation but again it doesn't seem to do anything other then make the player jump. I have gravity on(which from what I've seen in tutorials, should make the player sink when in the air but not stop flight)
I have also tried If(Input.GetKey("Space")) and If(Input.GetKey("Jump")) but none seem affective either P:
I don't actually remember how the built-in Character Controller handles motion, but it's very possible that any changes to its motion you're making are being overwritten by the other script. If we don't solve it now, take a good look around inside that script.
Just from glancing at this, it looks like what holding the spacebar should do is change the controller's verticalVelocity ins$$anonymous$$d of its transform.position.
Having your own script make changes to the char controller's data might work out fine, but just to be clear, my suggestion was to edit a copy of the built-in char controller script itself. But either way you're learning, so it's all good. :)
Your answer
Follow this Question
Related Questions
Hitting / Mining / slashing ? 0 Answers
How to make player move relative to camera 0 Answers
Jumping Function in my Player Controller Script isn't working 2 Answers
Starfox Zero-like airship movement help please :) 0 Answers
3D Floating Enemy Pathfinding 0 Answers