Hey guys so basically im trying to up the difficulty by making my enemies in this GAME to turn towards the player to make them harder to dodge. Atm I have a few errors that I am having problems with. For starters I made a script that should make it so that the enemy looks at the player so it turns toward it, this is what im trying to say:
So I just want it to be tracking the player and move towards it, not just strait down. This is the script for that:
var target : Transform;
var damp = 5.0;
function Start () {
// Auto setup player as target through tags
if (target == null GameObject.FindWithTag("Player"))
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
if (target Vector3.Distance(transform.position, target.position) < 30) {
var rotate = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
}
}
Another problem is when I press play to test the game in the editor, the script disappears form the game object, and to use it I have to add is during the game. the second problem (This is the main problem I need fixed atm), is that it is looking behind itself so basically it is not looking correctly. Here is a video showing what im saying.
Also for purpose of knowing how to change my script, this is how my game object is setup, so basically I am trying to make it look on teh Y axis, or I could be wrong (im still a noob).
I’ve got to head out right now, but I tried that out, and want to say that it was pretty cool and has potential.
However, you shouldn’t have that bit about F5. F5 does nothing in my browser, for example. Command- or Control-R is more universal (for Refresh, instead of F5, which makes no sense in English), but just telling people to refresh the page would be better, until you add more functionality.
LookRotation points the Z axis in a specified direction, so you’ll have to make your ship a child of a new gameobject oriented such that the ship’s nose points along the new object’s Z axis, then rotate the new object instead of the ship graphic.
Or alter your ship graphic directly to align its nose along its own Z axis.
Replace ‘transform.right’ by ‘transform.up’ depending on which axis is pointing towards the front of your ship.
Note that it is convention to have the front of your model point along the blue axis (it is called forward after all :P) and functions like LookAt and LookRotation assume that that is indeed the case.
@The OP: Unity does everything it can to impose the ‘y up’ convention. It’s certainly possible to use another convention (such as z up), but you’ll generally find yourself working around Unity’s conventions in that case.
For my 2-d (or 2-d-ish) Unity projects, I usually use a z-up coordinate system. Models point along the x axis in local space, and anything like LookRotation() that assumes a different convention, I just work around or implement manually.
You can’t assign to down directly, but you could aim for the opposite of the desired direction to achieve that result (note the minus inside the Lerp function):
I would, however, recommend fixing all your ship models to be aligned with the blue axis instead. I realize you would have to fix a lot of scripts you already have, but it will make writing future scripts much easier. Having to take into account just this one extra rotation makes coding rotation significantly more complex, as you have seen already.
@The OP: Sorry, I didn’t realize you were asking about assigning to the vector (in which case ‘-up’ obviously doesn’t do you much good).
@The OP: Opinions obviously differ on this, but I don’t think there’s any reason you’d need to change how your models are oriented. All the rotation-related functionality that the Unity API provides is fairly straightforward, and for the most part, any of the functionality that doesn’t work as-is in a z-up environment can easily be implemented manually if need be. (As I mentioned earlier, I generally use a z-up coordinate system for projects with a strong 2-d component, and have never had any problems with this.)
But, that’s just my experience - you should of course do whatever you think will work best for your project.