- Home /
Problem is technically not solved, but I found a better alternative to what I originally wanted to do.
Making a gameObject face at another gameObject?
So I am currently working on a 2D game, and trying to make an enemy that shoots bullets. These bullets are supposed to point at the players position, and then go to that position (not follow the player, just go to whatever the players position was when they are instantiated). However, I can't seem it get it to work: the bullet always rotates by -90 degrees on the x axis, and because I am making a 2D game, that causes the bullet to disappear from view. Does anyone have any idea of how to do this, or do it in a more efficient way? I have tried to use transform.LookAt and Quaternion.LookRotation.
Thank you!
Edit: Here is my code, except for the part where the bullet damages the player cause that works fine: Note: My code is quite messy because I have been trying a lot of different things, and I want to remember all of them, so I am keeping it like this just in case if I want to try something out again.
public class BossWeaponController : MonoBehaviour{
public GameObject Player;
Transform playerTransform;
Transform target;
Vector3 xAxisDirection;
Vector3 yAxisDirection;
Rigidbody2D myRB;
public float bossBulletSpeed;
public float damage;
void Start()
{
Player.transform.right = xAxisDirection;
Player.transform.up = yAxisDirection;
Transform playerTransform = Player.GetComponent<Transform>();
Rigidbody2D myRB = GetComponent<Rigidbody2D>();
// target = new Vector3(Vector3.forward, yAxisDirection);
// transform.Rotate(Vector3.forward * -90);
target = Player.transform;
//This is where I change the rotation
Vector3 relavtivePos = playerTransform.position;
// Quaternion rotation = Quaternion.LookRotation(Vector3.forward,relavtivePos);
transform.LookAt(target, Vector3.up);
// transform.rotation = rotation;
}
// Update is called once per frame
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, target.position, bossBulletSpeed);
}
Answer by prof · Jan 06, 2021 at 05:47 AM
That's because you're using 2d vectors for caclulating Quaternion. Use Vector3 or use different method for calculating rotation
As for LookRotation, if you have 2D set in XY plane, then your up vector would be on Z axis (Vector3.forward)
For the first part, yeah, I just realized I had forgotten to update the script as I was doing some research on the problem yesterday. Currently, I fixed the problem where it was rotating in such a way where it was unable to be seen. However, it still isn't facing the players.
For the second part, I don't fully understand, could you elaborate? I will update my code now.
You need to remember that unity is 3D engine. transform.LookAt(target, Vector3.up) does work, put in update() switch editor to 3D mode and drag around your object in play mode. You'll see how it's rotating.
As for solution, put your script on empty object, put inside that object another object with bullet sprite and rotate that sprite -90 on Y axis. (With default unity set up, LookAt(target, Vector3.up))
I had tried that; I tried what seems like everything. However, I thankfully found what I believe to be a better alternative to that; making the bullets rotation random(ish, there is a range), instead of it facing the player. I like it because it adds a sense of unpredictability to the game and keeps the players on their toes. However, I seem to have run into another problem: the bullet isn't moving, despite me telling it to. I have tried to do Rigidbody2d.velocity, and Rigidbody2D.AddForce, and even tried to use Raycasts to tell the bullet where to go, but it seems to not do any of those. I checked it's velocity and it appears to be 0, somehow. Do you know why this could be happening? If this problem pursues then I'll probably make a new question for it.
Answer by Llama_w_2Ls · Jan 04, 2021 at 08:37 PM
Have you tried Vector2.MoveTowards()
? Vector2.MoveTowards Documentation
Moves a point current towards target.
When the bullets are instantiated, you would need to create a new Vector2 that contains the current player position and set that as the target. This will mean that the variable will not update accordingly to the player's moving position, and it will go in a fixed trajectory. @BlueFFlame
Yes, that part does work: I went into 3D mode and saw that the bullet was in fact going to the players position. However, you just can't see it. The reason that I want to change it's rotation is so it looks like it is flying at the players. I'll update the code so you can see my whole script.
You can always reset the localEulerAngles of the bullet every frame, so that the rotation is the same but always 0 on the x-axis.
Thank, I will try that. How would you do that? I have an idea, but I haven't really worked with angles, Euler angles, Quaternions or anything like that so I am still quite unfamiliar with that topic.
Follow this Question
Related Questions
LookAt inaccurate rotation issue (javascript) 1 Answer
Lock rotation axis? 4 Answers
Rotation using Unity2D 3 Answers