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
3
Question by eddie987321 · Apr 13, 2012 at 11:13 AM · playerballrolltowards

ROLL a ball towards the player

Hi guys, I'm having a bit of trouble trying to get a ball to move towards the player while maintaining a rolling effect. Right now I'm using transform.LookAt which is basically freezing the X and Z rotation of the ball. I've tried making a separate object which looks at and moves towards the player and then having a mesh (without the rigidbody and collider) of the ball with a script to follow the separate object but this caused resulted in a very odd effect where the ball just seemed to rotate randomly and move straight in the positive X direction. The second I deleted the ball, the separate object I created works perfectly fine. I'm really lost, any ideas?

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
2

Answer by Datael · Apr 13, 2012 at 06:31 PM

I hope you don't mind C# but I just whipped this up for you to take a look at. I'm not sure if you did actually want to go with rigidbodies for it or not but if you did I hope this can help. You just need to attach it to the ball and set the object of attraction to the player. You can also choose what type of attraction you want it to be, although it's very basic... I hope I've understood your question correctly with this...

As long as your physics materials are set up to have friction (you can set up a default material in the Physics settings) the ball will also roll as it is moving towards the target.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody))]
 public class AttractedObject : MonoBehaviour {
     
     [SerializeField] Transform objectOfAttraction;
     [SerializeField] AttractionType attractionType;
     [SerializeField] float attractionStrength;
     [SerializeField] bool useSqrtOfDistance;
     
     Transform myTransform;
     Rigidbody myRigidbody;
     
     void Awake() {
         // cache these
         myTransform = transform;
         myRigidbody = rigidbody;
     }
     
     void FixedUpdate() {
         
         // get the positions of this object and the target
         Vector3 targetPosition = objectOfAttraction.position;
         Vector3 myPosition = myTransform.position;
         
         // work out direction and distance
         Vector3 direction = (targetPosition - myPosition).normalized;
         float distance = Vector3.Magnitude(targetPosition - myPosition);       // you could move this inside the switch to avoid processing it for the Constant case where it's not used
         
         // apply square root to distance if specified to do so in the inspector
         if (useSqrtOfDistance) distance = Mathf.Sqrt(distance);
         
         Vector3 resultingForceAmount = Vector3.zero;
         // depending on which type of attraction, work out the appropriate
         // amount and direction of force to apply to cause movement
         switch (attractionType) {
         case AttractionType.Constant:
             resultingForceAmount = attractionStrength * direction;
             break;
         case AttractionType.DecreaseWithDistance:
             resultingForceAmount = attractionStrength * direction / distance;
             break;
         case AttractionType.IncreaseWithDistance:
             resultingForceAmount = attractionStrength * direction * distance;
             break;
         }
         
         // then finally add the force to the rigidbody
         myRigidbody.AddForce(resultingForceAmount);
         
     }
     
     enum AttractionType {
         DecreaseWithDistance, // (like gravity)
         Constant,             // constant force magnitude
         IncreaseWithDistance  // (opposite of gravity)
     }
     
 }
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 Berenger · Jun 07, 2012 at 02:46 AM 0
Share

eddie987321 : Thank you so much! You went into more detail than I actually needed but I could still understand it and apply it in javascript

The key was to find a way for the ball to find the direction of the player without using LookAt but I had no idea how to do that until now.

zorrojusto : This is a great script for every one that need an Enemy attacking the Player with a rolling ball. Thanks a lot Datael!

$$anonymous$$e : When useSqrtOfDistance is true, the sqrt is applied twice. You need to replace magnitude by sqr$$anonymous$$agnitude. Btw, there is no function $$anonymous$$agnitude in Vector3. And guys, please use comments.

avatar image ShardDuke · Aug 25, 2013 at 10:42 PM 0
Share

I tried that script, but somehow the ball swings strangely back and forth, when I apply that script...

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

7 People are following this question.

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

Related Questions

Best way to achieve Sonic style physics? 0 Answers

the code work bat is not push the ball but with speed of the character ? 2 Answers

Controllable ball rolling away by itself 2 Answers

Design 3D ping pong using unity 2 Answers

How to roll a ball? 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