x


Keep an object on the gound.

Hello, I am using this script and the object I apply this script to floats in the air as it attempts to follow my character. How can I get the object to remain on the terrain as it follows my character? (the character changes heights on the terrain)

Thank you.

var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning

var myTransform : Transform; //current transform data of this enemy

function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
     target = GameObject.FindWithTag("Player").transform; //target the player

}

function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;


}
more ▼

asked Oct 12 '12 at 03:09 AM

sjj100 gravatar image

sjj100
1 1 2 4

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I had the exact same problem sometime ago, here is the solution:

The object is floating in the air because it is following the center pivot of your character (which is probably higher than your follower), if you want it to stay on the ground, just create an empty gameObject put it at the height of your character follower, but close to your character and attach it to him, add a tag to it (like targetPoint) and instead of looking for the player, look for that point:

target = GameObject.FindWithTag("targetPoint").transform; //target the player

Hope it Helps :)

more ▼

answered Oct 12 '12 at 03:21 AM

Noah 1 gravatar image

Noah 1
1.1k 33 45 50

This would work if my player was moving at a constant height, but it is varying. Thank you for the response.

Oct 12 '12 at 03:44 AM sjj100
(comments are locked)
10|3000 characters needed characters left

Easiest way is, you can make a raycast from your object to Vector3.down so you can find the point to snap on each frame.

Edit: I have not test it but possibly similar solution like this: Edit2 : I have changed the code. i rewrite it for java script. it would still give some possible errors, notify me further ;)

function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    //EDIT DONE HERE
    RaycastHit myHit;
    var offsetAlign:float = 0.0; //Add offset to make your object align perfect to terrain
    if(Physics.Raycast (myTransform.position, Vector3.down, myHit))
    {
         myTransform.position.y = myHit.point + offsetAlign;
    }

    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

}
more ▼

answered Oct 12 '12 at 03:23 AM

NowhereStudios gravatar image

NowhereStudios
36 1 2 3

I seem to be encountering some compile errors with NowhereStudio's answer... (24,15) ';' expected. Insert semicolon at the end. (25,10) ';' expected. Insert semicolon at the end.

Oct 12 '12 at 03:55 AM sjj100

try to add only "edit done here" written block to your previous not error giving update function

Oct 13 '12 at 09:53 PM NowhereStudios

aha i found i guess you are not using csharp i edited the post above

Oct 13 '12 at 09:54 PM NowhereStudios
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1472
x1093
x72
x40
x7

asked: Oct 12 '12 at 03:09 AM

Seen: 359 times

Last Updated: Oct 13 '12 at 09:57 PM