- Home /
Stick an object to the ground while moving?
Hi everyone,
I'm trying to make a basic movement engine for a rpg game without using the character controller or a rigidbody. The reason I don't want to use them is because there is no element of gravity in my game (you can't jump or fall). I am trying to find the best way to get my player (a cube that can move in 8 directions) to stay attached to the ground, (which has sloped areas as well as flat) while moving (without using gravity). What would be the best way to go about this?
I have looked around the web for possible solutions and found that "raycasting" may have been used for this, but I have no idea how I would set that up.
Thanks in advance! (Sorry for all the parenthesis :I )
Yes use Physics.Raycast() like describes in the docs! You will have to use RaycastHit to see RaycastHit.point for the target coordinates
Answer by tigerfoot · Oct 07, 2012 at 06:19 AM
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
var distanceToGround = hit.distance;
//use below code if your pivot point is in the middle
transform.position.y = hit.distance - transform.collider.bounds.extents;
//use below code if your pivot point is at the bottom
//transform.position.y = hit.distance;
}
}
Put this code in your objects movement script. It should work...
Your answer
Follow this Question
Related Questions
Teleporting To Back Of Object? 1 Answer
function OnCollosionEnter problems 1 Answer
Collide detection with tag [JS] 0 Answers
OnTriggerEnter Or OnCollisionEnter? 3 Answers
Please help with a simple collision. 3 Answers