in Education by
The problem is that I cannot make a script that makes the enemy rotate so that "up" is pointing against the player, (with that I mean so that it will come to the player with transform.up) And I have not found any working solution yet, (from what I can tell) I am trying to make a small 2D game, just for my friends to play. What I have tried is making the enemy look at the player and then set the "z" rotation to the same as the "x" rotation and after that reset the "x" and "y" rotation (so that "up" is pointing at the player). But the enemy just goes straight up. public class Enemy : MonoBehaviour { public Transform Player; // The Transform of the Player that you can controll public float MoveSpeed = 4; // How fast the enemy should go void FixedUpdate() { transform.LookAt(Player); transform.rotation = Quaternion.Euler(transform.rotation.x, 0, transform.rotation.x); transform.rotation = Quaternion.Euler(0, 0, transform.rotation.z); transform.position += transform.up * MoveSpeed * Time.deltaTime; } } So what I want to happen is that the Enemy will move to the Player but the rotation part is not working and it just goes straight up in the air instead of following the player. I might just be stupid and have missed something simple... Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
The easiest way I can think of is to make an empty GameObject, that will be the parent of your mesh. Then, rotate the child (your mesh), so that the part you refer to as "UP", is in-line with the parent's transform.up(positive y-axis). Once you've done that, you can simply use transform.LookAt(character), but using the parent object's transform. It depends a bit on how exactly you want the enemy rotated in the end but you could simply use Transform.RotateAround like transform.LookAt(Player); transform.RotateAround(transform.position, transform.right, 90); LookAt by default results in rotation such that transform.forward is pointing at the target, transform.right remains pointing right(ish) and transform.up remains pointing up(ish). So all you have to do is rotating around that local X-axis transform. right exactly 90° so now transform.forward points down instead and transform.up points at the target. which results in If you then also want to change the other axis you can still additionally rotate it around the local transform. up axis as well e.g. to flip the forward vector "upwards": transform.LookAt(Player); transform.RotateAround(transform.position, transform.right, 90); transform.RotateAround(transform.position, transform.up, 180);

Related questions

0 votes
    I'm working through my AI textbook I got and I've come to the last homework problem for my section: "Implement the ... in C# or Java? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm trying to implement stochastic gradient descent in MATLAB however I am not seeing any convergence. Mini-batch ... cost of 420. Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I am currently working on an AI Agent that will be able to identify both the start state and the goal ... be greatly appreciated. Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    From what I've read so far they seem very similar. Differential evolution uses floating point numbers instead, and ... of both. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I write programs to play a board game variants sometimes. The basic strategy is standard alpha-beta pruning or ... human players. Select the correct answer from above options...
asked Jan 29, 2022 in Education by JackTerrance
0 votes
    I try to learn and implement a simple genetic algorithm library for my project. At this time, evolution, ... Gaussian distribution?.) Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    In the MNIST beginner tutorial, there is the statement accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) tf ... (x,1)? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I am looking for an open source neural network library. So far, I have looked at FANN, WEKA, and OpenNN. Are the ... , and ease of use. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    What is the difference between informed and uninformed searches? Can you explain this with some examples? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Like lots of you guys on SO, I often write in several languages. And when it comes to planning stuff, (or ... to this being possible? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm looking for some examples of robot/AI programming using Lisp. Are there any good online examples available ... in nature)? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm teaching a kid programming, and am introducing some basic artificial intelligence concepts at the moment. To begin ... and boxes)? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I am searching for information on algorithms to process text sentences or to follow a structure when creating sentences ... be great. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm looking to try and write a chess AI. Is there something I can use on the .NET framework (or maybe ... making a chess game? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm writing a game that's a variant of Gomoku. Basically a tic tac toe on a huge board. Wondering if anyone ... [self put randomly]; } Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
...