How do I stop small animated models from falling to the ground?
Basically I've made some butterflies for my game with an animator that makes them flutter around in the air and head towards the player, except instead of flying towards the player, they basically face-plant into the ground and hop over towards the player.
While this is hilarious, I just want to have it so the butterflies fly towards my player, here's the script I'm using in case anyone knows how to modify it so they stay in the air:
// Use this for initialization
void Start () {
}
//The target player
public Transform Basehuman01b;
//At what distance will the enemy walk towards the player?
public float walkingDistance = 10.0f;
//In what time will the enemy complete the journey between its position and the players position
public float smoothTime = 10.0f;
//Vector3 used to store the velocity of the enemy
private Vector3 smoothVelocity = Vector3.zero;
//Call every frame
void Update()
{
//Look at the player
transform.LookAt(Basehuman01b);
//Calculate distance between player
float distance = Vector3.Distance(transform.position, Basehuman01b.position);
//If the distance is smaller than the walkingDistance
if(distance < walkingDistance)
{
//Move the enemy towards the player with smoothdamp
transform.position = Vector3.SmoothDamp(transform.position, Basehuman01b.position, ref smoothVelocity, smoothTime);
}
}
Again any advice is greatly appreciated :)
Sounds like you have a rigidbody attached to each butterfly. Check the components of the butterflies for a rigidbody and disable gravity for it.
Answer by Bigiansen · Sep 10, 2016 at 02:23 PM
I've tested your script and it seems to work
Have you checked if the pivot point of the player is not below the ground?
I don't think so, the butterfly model is above the mesh I made for the environment, unless you mean that white grid is the default ground?
This is what the model looks like when the game isn't running if that helps:
I mean the pivot point of the player. Your butterfly is targeting the pivot point of the player (transform.position), so if the pivot point of the player is below the ground, the butterfly will just fly downwards.
Try something like this:
transform.position = Vector3.SmoothDamp(transform.position, Basehuman01b.position + new Vector3(0,2,0), ref smoothVelocity, smoothTime);
That + new Vector3(0,2,0) is a vertical offset for the target position. Try increasing/decreasing the Y value.
That worked, thank you so much! :D The only issue I'm having now is trying to line up the glowing effect with the butterfly but I should be able to fix that be re-positioning the light P: