- Home /
How to make an object does not "derive"...
Hello, I am making a construction system like in Rust or The Forest. Per example, in those game, when you place a campfire, it's in green, on the ground and is in front of your player.
So, I tried to do the seem... With Rigibody (i don't know if it's the good way). But, the problem with rigibodies are they "derive", there no longer in front of you and that's the biggest problem. How force the rigibodies to always be in front of you at some distance, on the ground (don't pass trough it) even if the ground is sloping?
PS: Sorry for the misspelling, i don't speak english very well.
I want to tell: the object always face the ground and is in front of me at 5 meter (per example). The object can be stuck at the ground it doesn't matter.
It is hard to understand. I suggest you edit your question. Under the english part, write your question in your own language, maybe someone here will understand or be able to use a translator.
Things to consider :
Camera.main.transform.forward
will give you a direction that is forward to the cameras facing. You can make this a position in world-space with :
var newPos : Vector3 = transform.position + ( Camera.main.transform.forward * someDistance );
To move a rigidbody to that position, use Rigidbody.$$anonymous$$ovePosition
To match the objects rotation, you could use something like :
myObject.transform.rotation = transform.rotation;
or Quaternion.LookRotation, it relly depends on what you want.
I've editing my post. I'll try what you say tomorow.
EDIT: it doesn't work too, the object still "derive", i think the RigiBody.$$anonymous$$ovePosition doesn't work very well...
Why is there a rigidbody?
Rigidbodies are supposed to bounce and roll like real objects. When you add one, you're telling Unity to make the campfire slide down the slope and hit a tree.
If you don't have a rigidbody, you can move the object in the script. And it will stay where ever you put it.
Answer by Loius · May 18, 2014 at 04:49 PM
Something like this on the object that is "you" should work:
public Transform theObjectYouArePlacing;
public float distance = 5f;
public float distanceFromObjectPivotToBottomOfObject = 1f;
void Update() {
RaycastHit rch;
if ( Physics.Raycast(transform.position, transform.forward, out rch, distance) ) {
theObjectYouArePlacing.position = rch.point;
} else {
theObjectYouArePlacing.position = transform.position + transform.forward * distance;
}
theObjectYouArePlacing.rotation = Quaternion.identity; // reset tilt
theObjectYouArePlacing.forward = -transform.forward; // look at this object
if ( Physics.Raycast(theObjectYouArePlacing.position, Vector3.down, out rch, distanceFromObjectPivotToBottomOfObject) ) {
theObjectYouArePlacing.up = rch.normal; // align to ground
}
}
You might have to change the order of rotations to get the object to face the right way.
It doesn't work... I've got an error at line 14 (theObjectYouArePlacing is a Transform, not a vector3) so i change it in a vector3 with a transform.position and the Object i want to place stay stuck in my head! why???
Sorry, that was my fault.
It should be "theObjectYouArePlacing.position"
$$anonymous$$mh, it doesn't work, the object stay stuck in my head. I've put the script in the Camera... that's good???
I'm not sure. You can debug it - the rch variable has a collider which has a name:
Debug.Log(rch.collider.name); after a raycast and you can tell what the ray hit. You may need to change where the ray comes from, or have it skip some layers (see the documentation for raycast: http://docs.unity3d.com/Documentation/ScriptReference/index.html)
EDIT: "the object goes crazy, it does a round trip from the ground to me" -> the collider bug, so i remove it.
Now, it work perfectly. THAN$$anonymous$$S YOU!!!
Answer by Owen-Reynolds · May 19, 2014 at 11:09 PM
Maybe you should get some more practice with making easy things first. Rigidbodies with parents are usually just bad. With more practice, you can see why.
Use more normal child objects, and normal rigidbodies. Or, try putting something on the ground (and it just stays there) -- there's a way to find how high the ground is.
The problem is, i've do some easy things, i want to do this to continue the developpement easier (after that i want to do the AI and some 3D model). So, if it's done, i can continue without stress like "how i'll do this :/ ?"... I hope you understand what i mean.
Your answer
Follow this Question
Related Questions
Script not working after changing isKinematic 1 Answer
Shake Effect 0 Answers
Player Pushes Enemies. (That's bad) -Still Unanswered- 1 Answer