x


Bound the gameobject from the ground

Hey guys! Here's a complicated task I cannot solve and looking forward to get ur help!

Picture of what I want to get done : http://img180.imageshack.us/i/hitpoint.jpg/

Description : I've a character, When I left click with my mouse on the Terrain the GameObject(Also called - HitPoint) goes to the hit.point position that was casted from the Mouse's position. and that HitPoint gameobject goes there with his 0,0,0 rotation. and I want it to be rotated like 180 degrees from the terrain. -Because I cannot explain exactly what I mean about rotating it like 180 degrees from the terrain the picture will make sure u understood it right. On the beginning I did this :

var HitPoint : Transform;
movePosition = hit.point;
HitPoint.transform.position = movePosition;

Now I need to rotate the HitPoint's.transform.rotation somehow. Thanks in advance!

more ▼

asked Jun 21 '10 at 07:41 PM

AnaRhisT gravatar image

AnaRhisT
865 30 33 43

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

1 answer: sort voted first

If you have a terrain called "myTerrain", you can set the rotation of the object "myObject" (your cylinder) using this:

myObject.rotation = Quaternion.LookRotation(
     myTerrain.terrainData.GetInterpolatedNormal(
          myObject.transform.position.x / myTerrain.terrainData.size.x,
          myObject.transform.position.z / myTerrain.terrainData.size.z
     )
);

I've never actually done this before, so it's kind of a guess, but I'm fairly certain you do need to use GetInpterpolatedNormal(). If this exact code doesn't work, perhaps you can tweak it and figure it out yourself.

Also, this code doesn't use hitpoints for the purposes of making it more generic. This will actually work inside of Update(), and will cause "myObject" to always rotate with whatever direction the normal is at that point on the terrain.

more ▼

answered Jun 21 '10 at 10:27 PM

qJake gravatar image

qJake
11.6k 43 78 161

nope, doesn't work it's like the 0,0,0 is 40,0,0 .. 40 degrees on the x-axis..

Jun 21 '10 at 10:46 PM AnaRhisT

this is what i mean when i say 40 degrees on the x-axis : http://img130.imageshack.us/img130/6291/40degrees.jpg

Jun 21 '10 at 10:56 PM AnaRhisT

I also tried.. with a ray the normals..

movePosition = hit.point; normalPosition = hit.normal; HitPoint.transform.position = movePosition; HitPoint.transform.LookAt(HitPoint.transform.position + HitPoint.transform.forward, normalPosition);

Jun 22 '10 at 09:06 AM AnaRhisT
(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:

x2249
x2177
x1521
x124

asked: Jun 21 '10 at 07:41 PM

Seen: 1446 times

Last Updated: Jun 21 '10 at 10:29 PM