- Home /
C# store compare start position and actual position
Hello guys, I'm trying to make a script for a character to jump up to a certain height, then fall down. I'm using gravity to make it easier, but I'm having a little trouble finding how to limit the jump height.
I tried storing the starting y position of my player, then making a variable (toppos) that would be equal to 5 time the starting y as a limit. Then I want to compare the current y to that limit value and when they're equal the jump is off, and then when current y is equal to starting y again the jump can be used again. Still I must have done something wrong since no matter what I do and how high I go, the jump never goes off.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class Simple2DMoveController : MonoBehaviour {
public KeyCode Left = KeyCode.LeftArrow;
public KeyCode Right = KeyCode.RightArrow;
public KeyCode Up = KeyCode.UpArrow;
public KeyCode Down = KeyCode.DownArrow;
private Vector3 startPos;
private Vector3 toppos;
public float speed;
[HideInInspector]public float currentSpeed;
void Start () {
currentSpeed = speed;
rigidbody.constraints = RigidbodyConstraints.FreezePositionZ|RigidbodyConstraints.FreezeRotation;
}
//to set starting position when touching a platform
void OnCollisionEnter (Collision col){
if(col.gameObject.tag == "ground") startPos = transform.position;
}
void FixedUpdate () {
//bool to activate jump or no, declaration of toppos and check
bool canjump = true;
toppos = new Vector3 (startPos.x, startPos.y + 4, startPos.z);
Debug.Log (toppos);
rigidbody.velocity = Vector3.zero;
if(Input.GetKey(Left)){ rigidbody.AddForce(-Vector3.right * speed, ForceMode.VelocityChange);
}
if(Input.GetKey(Right)){ rigidbody.AddForce(Vector3.right * speed, ForceMode.VelocityChange);
}
//check if canjump is true
if (canjump){
if(Input.GetKey(Up)){ rigidbody.AddForce(Vector3.up * speed, ForceMode.VelocityChange);
//supposed to set canjump to false when toppos is reached
if (transform.position == toppos) canjump = false;}
}
//supposed to set canjump to true once ground is touched again
if (transform.position == startPos) canjump = true;}
}
Answer by britsplease · Apr 04, 2015 at 03:31 PM
The answer to your question is that you should also check that your character jumped higher than the limit. This is because it is very likely that there will be no frame where your character jumped exactly 4 units, it will be under it in one frame and in the next frame it will be above it, in most cases. So use transform.position.y >= toppos.y
.
On the other hand, if use check whether the ground is touched by transform.position == startPos
, then your character will be able to jump again only if it didn't move sideways and fell back to the exact position. The reason is that this way you check each coordinates of the Vector3
position. For height you should only check the y
coordinate.
Also your idea of saving the starting height for checking whether the player is on the ground is not really robust and works only when your map is flat. For example when you jump up to a platform, you won't be able to jump again since the coordinates won't match. Using raycasting would be a better idea.
I modified the script as you said, and it worked, thanks. I wouldn't know about raycasting, $$anonymous$$cher haven't taught us those yet (I'm in a video game school) He gave us this assignment to see if we could do it and how, and he told us he'll give us the real solution next class.
It might be raycasting XD
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
If gameobject moves do this 1 Answer
How can i make my enemy jump with a delay 1 Answer
Game Object to Transform Position 1 Answer