Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by VSuper · May 18, 2014 at 02:51 PM · physicsrigidbodygroundconstruction

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.

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · May 18, 2014 at 02:51 PM 0
Share

How does an object "always face the ground AND me"?

avatar image VSuper · May 18, 2014 at 04:05 PM 0
Share

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.

avatar image AlucardJay · May 18, 2014 at 05:18 PM 0
Share

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.

avatar image VSuper · May 18, 2014 at 08:14 PM 0
Share

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...

avatar image Owen-Reynolds · May 19, 2014 at 03:13 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VSuper · May 19, 2014 at 11:28 AM 0
Share

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???

avatar image Loius · May 19, 2014 at 03:10 PM 0
Share

Sorry, that was my fault.

It should be "theObjectYouArePlacing.position"

avatar image VSuper · May 19, 2014 at 05:25 PM 0
Share

$$anonymous$$mh, it doesn't work, the object stay stuck in my head. I've put the script in the Camera... that's good???

avatar image Loius · May 19, 2014 at 06:15 PM 0
Share

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)

avatar image VSuper · May 21, 2014 at 06:08 PM 0
Share

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!!!

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VSuper · May 20, 2014 at 02:30 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Script not working after changing isKinematic 1 Answer

Shake Effect 0 Answers

Player Pushes Enemies. (That's bad) -Still Unanswered- 1 Answer

Use force to move to position of object 1 Answer

transform.Rotate and Rigidbody.MovePosition 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges