- Home /
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?
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)
}
}
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.
I tried that script, but somehow the ball swings strangely back and forth, when I apply that script...
Your answer
Follow this Question
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