- Home /
Rotate a 2D sprite towards target sprite?
Hi guys !
i am making a 2d space shooter . I've got two spaceships , one for player and other for enemy .It's a top down shooting game . I have the player facing top direction and enemy ship facing player that makes enemy at Rotation.z =180 . (upside down)
I'm trying to rotate the enemy sprite towards the player current position and transform towards it .the rotation should be only withing a certain angle window ...i mean it shouldn't be 360 degree rotation..just a sector of 120 degrees .....
But i'm unable to rotate it the way i want...the rotation is not restricted to only z axis with the code i use presently ...
I've just started to learn unity and i ain't that good with trigonometry ...
So help is really needed folks ...
Here's a screenshot !
[1]: /storage/temp/4693-screenshot.png
LookAt is a very handy command. also "Rotate" and also "RotateAround"
hope helps!
ahh...i have tried all of these..but nothing seems to work for me...there's rotation but not as i wanted ...
This is what you are looking for :
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html
This script is set up so the object transform.forward is looking at the target, with the object transform.up facing to the the negative Z axis :
#pragma strict
public var thePlayer : Transform;
private var myTransform : Transform;
function Start()
{
if ( thePlayer == null ) {
Debug.Log( "thePlayer object is missing - " + gameObject.name + " Inspector" );
thePlayer = GameObject.FindWithTag("Player").transform;
}
myTransform = transform;
}
function Update()
{
var rot : Quaternion = Quaternion.LookRotation( thePlayer.position - myTransform.position, -Vector3.forward );
myTransform.rotation = rot;
}
After re-reading the question I don't think this is what you want. If I now understand correctly, you want to limit the rotation of the ship to within +- 60' of facing down, but still try and look at the ship?
Your answer
Follow this Question
Related Questions
2D sprite rotation with velocity and interpolation 1 Answer
Change Sprite Origin in 2D 1 Answer
Rotate 2D Sprite Towards Moving Direction 3 Answers
Sprite is a little of when rotating 1 Answer