Question by
kurpingspace · Nov 03, 2018 at 04:44 PM ·
jumpingplayer movement
How to detect when player is colliding with something?
So I'm trying to make a sphere jump when the spacebar button is pressed and if the player is touching something. eg, ground or on top of some cubes.
However I do not know how to detect a collision, I've researched through on how but all I get is code that only detects if it's touching a specific object, however, I want it to detect any object.
Here's my code.
using UnityEngine;
public class PlayerMovement : MonoBehaviour{
public Rigidbody player;
public Rigidbody cam;
public int MovementSpeed = 400;
bool jump;
//if colliding with something {
//jump = true;
//} else {jump = false;}
void Update () {
if(Input.GetKey("w")) { player.AddRelativeForce(0, 0, MovementSpeed * Time.deltaTime); }
if(Input.GetKey("a")) { player.AddRelativeForce(-MovementSpeed * Time.deltaTime, 0, 0); }
if(Input.GetKey("s")) { player.AddRelativeForce(0, 0, -MovementSpeed * Time.deltaTime); }
if(Input.GetKey("d")) { player.AddRelativeForce(MovementSpeed * Time.deltaTime, 0, 0); }
if(Input.GetKey("space")) {
if (jump) {
player.AddForce(0, 800, 0);
jump = false;
}
}
player.transform.forward = cam.transform.forward;
}
}
Comment
Your answer
Follow this Question
Related Questions
control jump height and jump number by touch 0 Answers
How to add a jump key 2 Answers
Changing shape of player jump path 0 Answers
How to make a jump directional 1 Answer
how to jump in parabola. 0 Answers