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;
}
}
2 Answers
2
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;
}
}
Cheers will let you know if it works 