I’m making a game where the player has a jetpack. He clicks to use the jetpack. I’m doing this with this code, where “upasdf” is the player’s y position and “jetpackSpeed” is a float.
if (Input.GetKey(KeyCode.Mouse0))
{
for (float i = upasdf; i <= upasdf + jetpackSpeed; i++)
{
if (i <= 700)
GameObject.FindWithTag("Player").transform.position = new Vector3(x, i, z);
}
}
The problem is that whenever I try to set “jetpackSpeed” to anything less than 1 (ie .5), it doesn’t work. It doesn’t give me an error, but it just won’t make my player go up. How do I fix this?
The section of code you gave here will do nothing if jetpackSpeed is less than 1 because the for loop’s continue statement will not evaluate to true.
What’s going on inside the computer:
Start for loop
assign: i = currentPosition
check : currentPosition less than or equal to currentPosition + #?
do body of loop
the first time the loop is performed nothing happens (position = currentPosition)
increment i
check : currentPosition + 1 less than or equal to currentPosition + #?
if number < 1, then no it is not
end loop
So that’s why it doesn’t work if # is less than 1. My question is Why are you using a loop?
Just do this:
DO NOT COPY-PASTE THIS CODE. TYPE IT OUT BY HAND AND MAKE SURE YOU UNDERSTAND EACH LINE BEFORE USING. Trust me, it helps.
//At top of file
var player : GameObject;
var jetpackSpeed : Vector3 = Vector3(0, 5, 0);//half of gravity upwards per second
//this is called once when the object turns on
function Start()
{
//GameObject.Find is an expensive operation, also the player
// object will remain the same Object, usually.
// Find it once and keep track of it.
player = GameObject.FindWithTag("Player");
}
//this function is called once every frame
function Update()
{
//check if the user activated the jetpack
if(Input.GetKey(KeyCode.Mouse0))
{
//move the player. We multiplying by deltaTime will make it so that we
// always add jetpackSpeed once every second instead of once every frame
player.transform.position += jetpackSpeed * Time.deltaTime;
}
}
You’re incrementing i in the for loop - it will become higher than upasdf+jetpackSpeed after one single loop.
Anyway, the whole idea is wrong - you are violating several Unity commandments:
1- thou shalt not stop Unity with a lengthy for loop (slows down your game);
2- thou shalt not move your character by setting position directly (collisions can’t be detected);
3- thou shalt not use any Find function inside loops or Updates (your game will become as fast as a snail);
Since I don’t know what is your character, I can suggest a simple way to do this jet pack effect - attach this simple script to the player:
public float jetpackSpeed = 2.0f; // speed in meters per second
void Update(){
if (Input.GetKey(KeyCode.Mouse0)){
// this will move the object up in a frame rate independent speed
transform.Translate(0, jetpackSpeed * Time.deltaTime, 0, Space.World);
}
}
This code still violates the second commandment, and no collisions will be detected. To solve this, I must know what is your character - CharacterController, rigidbody, or just a simple object.