- Home /
Deteching collsions when jumping when not using a rigidbody
Hi, I've recently started to mess around with Unity.
Atm I'm trying to create a simple movement script in javascript using a cube as the object I want to move. I don't want to make it a rigidbody however I cannot work out how to make it detect the floor when it "jumps".
Any feedback will be very useful, thanks.
#pragma strict
//movement variables
var moveSpeed: int = 2;
var backSpeed: int = 1;
var runSpeed: int = 3;
var rotationSpeed: int = 5;
//variables to do with jumping
var jumpHeight: int = 5;
var inJump: boolean = false;
var gravity: float = 20.0;
var move: Vector3;
function Start () {
}
function Update () {
// moving forward
if(Input.GetKey(KeyCode.W)){
move = Vector3.forward * moveSpeed;
transform.Translate(move * Time.deltaTime);
}
// jumps, makes it know its in the air
if(Input.GetKeyDown(KeyCode.Space)&& inJump == false){
move = new Vector3(0,jumpHeight,0);
transform.Translate(move * Time.deltaTime);
inJump = true;
}
//if it is currently jumping, apply gravity to it
if (inJump == true){
move.y -= gravity * Time.deltaTime;
transform.Translate(move * Time.deltaTime);
}
if (//NEEDS SOME SORT OF COLLISON DETECTION HERE ) {
inJump = false;
}
}
Answer by robertbu · Mar 23, 2013 at 07:21 PM
You have a number of choices:
If you are using a plane as your floor and are not worrying about landing on other objects, you can just use the 'y' position of your block. This obviously won't work if you are jumping from platform to platform.
You can so a Physics.Raycast(). down to determine how far below you is any object, and use what it returns as your floor. The issue here is that you typically only test the position directly below the center of the cube. So you will not detect a glancing hit on another object below you.
A Physics.SphereCast() may be what you are looking for. Since a sphere will not exactly match the shape of your object, it is not a perfect solution.
And there are other solutions. Below I made changes to your code to use a SphereCast(). There are a significant number of changes.
#pragma strict
//movement variables
var moveSpeed: int = 2;
var backSpeed: int = 1;
var runSpeed: int = 3;
var rotationSpeed: int = 5;
var objectHeight = 0.49; // Distance between center point of the object and the ground.
var objectSize = 0.5; // Raidus of the sphere cast
//variables to do with jumping
var jumpHeight: int = 5;
var inJump: boolean = false;
var gravity: float = 20.0;
var move: Vector3;
var ground = 0.0;
function Update () {
// moving forward
if(Input.GetKey(KeyCode.W)){
move = Vector3.forward * moveSpeed;
transform.Translate(move * Time.deltaTime);
}
// jumps, makes it know its in the air
if(Input.GetKeyDown(KeyCode.Space)&& inJump == false){
move = new Vector3(0,jumpHeight,0);
transform.Translate(move * Time.deltaTime);
inJump = true;
}
//if it is currently jumping, apply gravity to it
if (inJump == true){
move.y -= gravity * Time.deltaTime;
transform.Translate(move * Time.deltaTime);
}
var hit : RaycastHit;
if (Physics.SphereCast(transform.position, objectSize, Vector3.down, hit)) {
//Debug.Log("-->"+hit.collider.name);
ground = hit.point.y + objectSize;
}
if (ground >= transform.position.y){
inJump = false;
transform.position.y = ground;
}
}
[3]: http://docs.unity3d.com/Documentation/Components/Layers.html
Your answer
Follow this Question
Related Questions
How to move multiple dynamic gameobjects with one moving platform which is controlled by the player 0 Answers
How do I move this rigidbody? 1 Answer
How to get character movement done with box collider 2d? 2 Answers
Transforming position for different colliders for repeating background? 0 Answers
How to stop a character from moving out of player's control? 0 Answers