- Home /
Still constant jumping even with raycasting.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float jumpingForce = 75f;
public bool canJump;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKey("right")){
transform.position += Vector3.right * 5f * Time.deltaTime;
}
if(Input.GetKey("left")){
transform.position += Vector3.left * 5f* Time.deltaTime;
}
if(Input.GetKey("up")){
transform.position += Vector3.forward * 5f * Time.deltaTime;
}
if(Input.GetKey("down")){
transform.position += Vector3.back * 5f * Time.deltaTime;
}
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 1.01f)); {
canJump = true;
}
if(canJump && Input.GetKeyDown("space")) {
canJump = false;
GetComponent<Rigidbody> ().AddForce (0, jumpingForce, 0);
}
}
}
, using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour
{
public float jumpingForce = 75f;
public bool canJump;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKey("right")){
transform.position += Vector3.right * 5f * Time.deltaTime;
}
if(Input.GetKey("left")){
transform.position += Vector3.left * 5f* Time.deltaTime;
}
if(Input.GetKey("up")){
transform.position += Vector3.forward * 5f * Time.deltaTime;
}
if(Input.GetKey("down")){
transform.position += Vector3.back * 5f * Time.deltaTime;
}
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 1.01f)); {
canJump = true;
}
if(canJump && Input.GetKeyDown("space")) {
canJump = false;
GetComponent<Rigidbody> ().AddForce (0, jumpingForce, 0);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Sphere can jump, but doesn't stop. 1 Answer
Cant jump while running left or right c#? 3 Answers
3D coyote time 1 Answer
Not able to jump 1 Answer
Distribute terrain in zones 3 Answers