• 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 /
  • Help Room /
avatar image
0
Question by --Brad-- · Mar 19, 2016 at 05:23 PM · c#2dmouseobjectsplatform

Get Velocity of Rigidbody Object Below Another Object

Hi all, so I'm having trouble trying to work this one out on my own.

Basically I have a 2D platform that will be moving to carry objects on-top of each-other. I need those objects to 'look' directly below them (even if they're rotated in real-world space), so it's always looking down relative to the world, not the object.

I've seen similar things done with Rays but I have no idea how to use them (most I've seen are in JavaScript and I need C#), especially in this context.

So the platform and the objects are all rigidbodies and I'm modifying the velocity of the platform, I've got them so they move together currently, but that's due to the platform's rigidbody velocity and the high friction, but they fall off too easily.

What I need is for them to also gain the same speed in the correct direction, but minus a few 0.1f's to give a bit of a 'sway' effect.

This is the code I'm using for the platform (no code written for the objects themselves yet):

 void Update()
 {
 
     float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
 
     Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
 
     mouseDelta = pos_move - lastMousePosition;
 
     acceleration = mouseDelta.x * 0.4f;
     platformVelocity += acceleration;
     platform.velocity = new Vector2(platformVelocity, 0) * speed;
 
     lastMousePosition = pos_move;
 
 }

All help is very much appreciated! Thank you!

Comment
Add comment · Show 1
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 --Brad-- · Mar 20, 2016 at 12:32 AM 0
Share

Any help at all guys? I'm pretty stuck.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help me with gui please (C#) 2 Answers

Child an Object to Another Objects Parent On Collision 1 Answer

No overload for method 'fireBullet' takes 0 arguments 1 Answer

Help with weapon rotation top down 2D C# 0 Answers

Rotate towards mouse pointer 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges