- Home /
manipulating position.y in unity
Hello, i'm trying to create script that will push player to the sky and if it reach equal lvl, it will fall down.
Here's part of code
if (controller.isGrounded) {//if controller is on the ground
isFlying=false;//set that it is not flying
if (Input.GetButton ("Jump"))//if button jump
{
isFlying=true;//the thing started to be in air
jumpReached = false;//but it doesnt reach the max altitude
jumpStarted=false;//the jump mechanism is not started yet
}
}
if (isFlying == true && jumpReached==false && jumpStarted ==false) {//if thing is flying, the altitude is not reached and jump procedure is not started
moveDirection.y += 0.9F;//set object 0,9 above ground block
maxJump = moveDirection.y + 10F;//set altitude 10 blocks above ground
jumpStarted = true;//initial jump procedure ended
}
else if(isFlying==true && jumpReached==false && jumpStarted ==true)//if thing is flying, altitude is not reached and jump has started
{
if (moveDirection.y >= maxJump)//if altitude reached
{
jumpReached = true;//now its time to fall down
maxJump = maxJump - 10F;//refreshing maxjump
}
moveDirection.y += 3F;//if not give the player strength to fly to the air ;)
}
if (isFlying==true && jumpReached==true) {//if player is in the air and it should fall down
{
moveDirection.y -= 0.95F;//decrease player position by 0.95
controller.Move (moveDirection * Time.deltaTime);
}
if(controller.isGrounded == true)//if player is on the ground
{
isFlying =false;
jumpReached = false;
jumpStarted=false;
maxJump = 0;
}//reset everything
}
moveDirection.y -= 0.60F;//if nothing happens players is going to fall
controller.Move (moveDirection * Time.deltaTime);
The problem is that I can't compare position of player to maxJump. Any idea?
You should probably move the comments on top of the rows they comment. The code is very hard to read like this.
The problem is that I can't compare position of player to maxJump.
Why ? What happens when you try to? Do you get an error or does the code not work as you expect it to ?
if (moveDirection.y >= maxJump)//if altitude reached
{
jumpReached = true;//now its time to fall down
maxJump = maxJump - 10F;//refreshing maxjump
}
it never starts
Answer by AsPhAlT · Jun 01, 2015 at 04:34 PM
Ok, done. I just create two transform value; one actual position of object and second - where is the limit. Secondly I used sqrMagnitude, and when the objects are too close it is going to fall.
Answer by Vice_Versa · Jun 01, 2015 at 03:58 PM
asuming thi script is attached to your player, use transform.position.y tyo refer to its height not moveDirection.y
Assets/Standard Assets/Characters/ThirdPersonCharacter/Scripts/movement.cs(146,45): error CS0120: An object reference is required to access non-static member `UnityEngine.Transform.position'
I miss something?
transform is spelled with a lowercase t. monodevelop autocorrects it for no particular reason