Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Pryzum · Jan 14, 2019 at 01:36 AM · rigidbodygravitylookatlookrotation

How to get RigidBodies to LookAt player, while also moving around the surface of a cylinder?

These two scripts (first attached to the cylinder, or tree, and the second to the Rigidbodies, or bugs) control the gravity of the bugs around the tree.

Attached to tree:

 public class TreeGravityScript : MonoBehaviour {
 
     public float gravity = -12;
     private Transform cylinderTransform;
     private Vector3 cylinderAxis = new Vector3(0, -4, 0);
     public Transform target;
 
     void Start()
     {
         cylinderTransform = transform;      
     }
    
     public void AttractBug(Transform characterTransform)
     {
         Vector3 gravityUp = Vector3.ProjectOnPlane(characterTransform.position - cylinderTransform.position, cylinderAxis);
         Vector3 localUp = characterTransform.up;
 
         characterTransform.GetComponent<Rigidbody>().AddForce(gravityUp * gravity);
 
         Quaternion targetRotation = Quaternion.FromToRotation(localUp, gravityUp) * characterTransform.rotation;
         characterTransform.rotation = Quaternion.Slerp(characterTransform.rotation, targetRotation, 50f * Time.deltaTime);
 
     }
 }

Attached to bug:

 public class BugGravityBody : MonoBehaviour {
 
     public TreeGravityScript attractorPlanet;
     private Transform characterTransform;
     public Transform target;
 
     void Start()
     {
         GetComponent<Rigidbody>().useGravity = false;
         GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
 
         characterTransform = transform;
     }
 
     void FixedUpdate()
     {
         if (attractorPlanet)
         {
             attractorPlanet.AttractBug(characterTransform);
 
 
         }
     }
 }

A simple Vector3.MoveTowards code in a separate script attached to the Bugs controls their movement towards the player. This is what the culmination of these scripts look like:

Bug_Follow_GIF

My problem is getting the bugs to also face towards, and look at, the player while still correctly moving around the tree. (Transform.LookAt does not work, and x-y-z constraints can't be added since movement around the cylinder changes all of these values). Any help would be greatly appreciated!

Comment
Add comment
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

1 Reply

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

Answer by Klarzahs · Jan 14, 2019 at 04:04 PM

Hi,

take a look at Quaternion.LookRotation(Up, Forwards). Up would be your inverse gravity /log normal, forward is the direction to the player, which is player.position - bug.position

Comment
Add comment · Show 2 · 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 Pryzum · Jan 14, 2019 at 11:59 PM 0
Share

Thanks for the feedback, I've looked into this and am still unsure of where in my code that this would go to be effective. I have tried inputting it inside and outside of my QuaternionSlerp line, but no difference is made in the bugs movement, any ideas?

avatar image Pryzum · Jan 15, 2019 at 02:42 AM 0
Share

Never$$anonymous$$d! I needed to replace the Quaternion.Slerp with the line you suggested. Thanks for the help!

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

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

Bullet not moving towards player 3 Answers

Gravity slower when moving Rigidbody 0 Answers

Add Gravitational Attraction to RigidBody2D? 3 Answers

Objects rotating after falling on another object with Physics & Gravity? 1 Answer

mecanim and rigidbody gravity effect doesnt work 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