- Home /
aim the enemy to me, Rotate(90 * Vector3.up)
Hello, i have this script that i've seen from a question here on unity answers, and i'm trying to adapt it to me, but a cant, i'm making a battleship game, and i need the enemy to target me, so he needs to be pointing at me, but since i've use someone else script, now i cant put Rotate(90 * Vector3.up), so that he looks lateral at me, can some one help? this is my script:
# pragma strict
var speed = 3000;
var distance : int;
var maxDistance = 50;
var myTransform = transform;
var LookAtTarget : Transform;
var bullitPrefab : Transform;
var damp = 6.0;
var savedTime = 0;
function Update ()
{
distance = (LookAtTarget.position - myTransform.position).magnitude;
if(LookAtTarget && distance < maxDistance)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); //this is what i want: transform.Rotate(90 * Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime * damp);
var seconds : int = Time.time;
var evenodd = (seconds / 2);
if (evenodd)
{
Shoot(seconds);
}
}
}
function Shoot(seconds)
{
if(seconds != savedTime)
{
var bullit = Instantiate(bullitPrefab,transform.Find("spawnPoint").transform.position,
Quaternion.identity);
bullit.rigidbody.AddForce(-transform.right * speed);
savedTime = (seconds);
}
}
Answer by Bunny83 · Jun 27, 2012 at 12:46 PM
If you just want to rotate the target rotation by 90°, just do something like that:
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
var r90 = Quaternion.Euler(0,90,0);
rotate *= r90;
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
You might need to use -90 but in general that should work.
ps: the whole evenodd thing doesn't work. You don't compute the modulo, you just divide the seconds. The if will be true as long as seconds is 2 or greater, which it is after two seconds...
Also there are a lot unnecessary calculations. Also you have a chached transform (myTransform) but you still use transform. Also distance and maxDistance are integer which doesn't make much sense. It also looks like you don't use the distance variable somewhere else in your script, so it should be just a local variable and not a member variable.
function Update ()
{
var targetDirection = (LookAtTarget.position - myTransform.position);
var distance = targetDirection.magnitude;
if(LookAtTarget && distance < maxDistance)
{
var rotate = Quaternion.LookRotation(targetDirection );
var r90 = Quaternion.Euler(0,90,0);
rotate *= r90;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, rotate, Time.deltaTime * damp);
var seconds : int = Time.time;
var evenodd = (seconds % 2); // modulo operator
if (evenodd)
{
Shoot(seconds);
}
}
}
@Bunny83 you have eli$$anonymous$$ated my shoot function, but i'm using that part in my script, you see this script is about rotating the enemy so that he "targets me" and then shoot at me, i also trynhg to make a hit system points, that when i hit the enemy boat 3 times he destroys himselve, after an animation was played, so i kinda need that part, but i've added you're part of the script to $$anonymous$$e, and it works just fine! thanks a lot man! :D
I did what? I just posted an slightly optimised Update function. I didn't posted your whole script... I still call your Shoot function so of course you need the rest of your script (your variables and your Shoot function)
Answer by FadeToBlack · Jun 26, 2012 at 10:40 AM
There is a few ways to do what you want:
you can multiply your rotation quaternion by another quat with needed rotation (there is function to construct quat by axis and angle, then you need to multiply var rotate by it - note what multiplication order is important). you need to know some math to do it right, or just make some experiments.
you can create dummy(empty) GameObject and link your model to it. (make children, i.e. drag your model at dummy in scene tree view). then you need to setup relative rotation in editor. this is the common way - any object can be adjusted, just make parent dummy object for it. also you can adjust center of rotation using this trick.
can you explain the second one a litle more simplier?
the GameObjects in unity can has hierarchy, i.e. one GameObject can inherit transformation of the parent GameObject.. hmm.. not so much simpler... so, imagine a door as Game Object & door handle as GameObject. if you turn door, door handle also will move, because it connected (linked) to the door. as this case, gameobjects can has some connections (hierarchy relationships like parent-child). when we move or rotate parent object, child object also moves, & this is the common gamedev practice used in many game engines & also 3d max has the same thing (select & link tool). you need to create empty gameobject and link your battleship gameobject to it. then you need rotate your battleship by 90 deg relative to empty gameobject. you need to apply lookat transformation to empty game object, not to the battleship. to make hierarchy you just need to drag & drop your battleship object on empty gameobject in hierarchy window.
but cant i just add this line to the script?
Rotate(90 * Vector3.up)
Isn't that simplier?
simple, simple. there is no simplicity at all. if there is a lot of battelships, you can be surprised that models will have different rotations (ye, modellers think another way). do you need kick modeller`s ass each time when it happens? no, just do it yourself!
Answer by Flufflesthepancake · Jun 27, 2012 at 11:33 AM
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position + Vector3.up*90)
Depending on which direction you want it to rotate you might minus it, or change it to one of the other vectors (right, forward...)
because we're dealing with vector3's and rotation here... silly me.
lot more simplier! :D But he says that " Operator '+' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'int'"..
Yeah, sorry, I posted a version that should work on the answer above:
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position + 90);
Because rotate is a vector3 and you can't just go adding numbers to them jazz.
yes i notest that, i've already tryed that, the same error appears, can use '+' operator.
and sorry but i didn't undestood you're 3 line, not so fluent at english, i'm portuguese!
any idea to solve this error?
Urgh, I'm not doing this well...
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position + Vector3.right*90); THAT, might do it.
yeah i've tryed this line before, i notest that! ;) but still the same problem resides, can join the + operator. and i'm sorry but i didnt understand you're 3 line. :P portuguese trying to speak english.. doing my best!
Any idea to solve this?
Your answer
![](https://koobas.hobune.stream/wayback/20220613073313im_/https://answers.unity.com/themes/thub/images/avi.jpg)