- Home /
How do I stop my enemy from falling over?
Hey there, I got this simple game in the process of being made, and so far I'm trying to create an enemy AI that follows the player and attacks them when you get to close. I got my enemy to follow the player, and for now I'm fixing any issues that occur once he gets close, and one of them involve my enemy falling over.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : MonoBehaviour {
private GameObject myTarget;
private Transform targetPos;
public float speed;
void Start ()
{
myTarget = GameObject.FindGameObjectWithTag("Player");
targetPos = myTarget.transform;
}
void Update ()
{
if(myTarget)
{
transform.position = Vector3.MoveTowards(transform.position, targetPos.position, speed * Time.deltaTime);
}
Vector3 lookVector = myTarget.transform.position - transform.position;
lookVector.y = transform.position.y;
Quaternion rot = Quaternion.LookRotation(lookVector);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, 1);
}
}
This is top to bottom of the code I got so far. I do have a Rigidbody attached to my enemy, and is most likely the reason their falling over; but they still fall over, but begin to float as well.
If it is 2D, just tick the "Freeze Rotation" box, at your "Enemy" Rigidbody! (It is located under "Constraints").
Try this one.
private GameObject myTarget; private Transform targetPos; public float speed;
void Start ()
{
myTarget = GameObject.FindGameObjectWithTag("Player");
targetPos = myTarget.transform;
}
void Update ()
{
if(myTarget)
{
transform.position = Vector3.MoveTowards(transform.position, targetPos.position, speed * Time.deltaTime);
}
Vector3 lookVector = myTarget.transform.position - transform.position;
lookVector.y = 0;
lookVector = lookVector.normalized;
Quaternion rot = Quaternion.LookRotation(lookVector);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, 1);
}
Answer by Ermiq · Mar 05, 2021 at 03:47 AM
There are many ways to properly rotate a transform or rigidbody on y
axis only. But to not rewrite too much of the existing code, you can just change
lookVector.y = transform.position.y;
to
lookVector.y = 0;
Why? Because the y
component here represents the height difference between enemy position point and a point at the direction line that points to the player. So, the closer the enemy gets to the player, the more significant the y
component becomes in the resulting direction vector. To eliminate this, you need to set the direction horizontal factor (the y
) to 0
, so the resulting look direction will be completely independent of the heights, and only the x
(left-right difference) and z
(forward-backwards difference) will be counted.
PS: when you tried to freeze the rotation by setting rigidbody constraints, you have constrained the rigidbody properties only. The thing is, rigidbody and transform are 2 different components on the game object. And the rigidbody's rotation constraints only affect the rigidbody. If you were manipulating the enemy with rigidbody special functions like AddForce()
to push the enemy with some physical impulse force or AddTorque()
to spin the rigidbody, the constraints would be applied to the resulting motion internally by the physics engine. But since you manipulate the transform directly, the rigidbody constraints are not counted at all.
Thank you so much!! I feel so ridiculous for having to ask someone to supply me the answer when it was so simple; I'm trying my best to understand all the terms and components used to make what you want the game to do.
Answer by xdragon-slayerx · Mar 04, 2021 at 02:39 PM
Do you have a colliders for enemies either? Have you checked your forward for enemies? it's Z axis arrow. Have you tried to set only y rotations for enemies? Now you are trying to rotate every axis.
I do have a Box Collider attached to my enemy. I have tested freezing every x, y, and z rotation, and they still end up falling over. I should probably rephrase what its doing though, because it looks like their falling over, but its more like tilting more and more facing upward the closer they get. They'll be fine the moment I start, but while they make their way towards me they slowly tilt upwards, and if you walk away, they start to pick themselves up. It's also facing me while walking on the z-axis.
Answer by Nyan_Cat123 · Mar 06, 2021 at 01:43 PM
maybe in the inspector freeze rotation in the rigidbody
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Flip over an object (smooth transition) 3 Answers
Player movement ignores some collision 0 Answers
problem with twin stick shooter controls 2 Answers