- Home /
I have a problem with my sidescroller movement script
I am still fairly new with getting my way around scripting in Unity. I recently had a finished character move script, but it had a few minor problems. The jumping was a bit difficult to script, and it seems as if my code could use some simplifying. This is my script now:
using UnityEngine; using System.Collections;
public class Lol : MonoBehaviour {
public float moveSpeed = 10f; public float rotateSpeed = 5f;
// Use this for initialization void Start () {
}
// Update is called once per frame void Update () { float moveForward = moveSpeed Time.smoothDeltaTime Input.GetAxis("Vertical"); float moveBack = moveSpeed Time.smoothDeltaTime Input.GetAxis("Horizontal");
transform.Translate(Vector3.forward moveForward); transform.Translate(Vector3.back moveBack);
if(Input.GetKeyUp(KeyCode.Space)){
Jump();
}
}
private void Jump () { rigidbody.velocity = new Vector3(0, 0, 0); rigidbody.AddForce(new Vector3(0, 500, 0), ForceMode.Force);
}
void FixedUpdate(){
grounded = Physics.Raycast(transform.position, -Vector3.up, 1.0f); }
} so the first noticeable complication is that the jump would be unlimited (meaning I could keep pressing space to boost my way up infinitely). I attempted to implement a simple bool isgrounded
like other similar movement scripts that involve jumping. Second problem is that the script could be more compact, for sake of performance.
Could anyone here demonstrate the proper way to add a system to where if the player is on the ground, he can jump, but cannot jump if he is already in the air? And if possible, could someone make this script a little more compact; I'm sorry if it seems a bit much to ask.
Thanks!
Your answer
Follow this Question
Related Questions
Better 2D Sprite Jump in 3D Plane 1 Answer
Multiple jump C# Script. 0 Answers
Multiple Cars not working 1 Answer
My jump charge script doesn't work. (C#) 0 Answers
Multiple jump C# Script. 2 Answers