- Home /
LookAt on only 1 axis for 2D games?
http://www.officegamespot.com/officegames/appleshooter.htm
Look at this game, the bow rotates based on where the mouse is
How can I make it? I've been trying for 5H (seriously!) If you know quaternions/eulers please help me
Here's my project (.unitypackage 0.5mb) http://www17.zippyshare.com/v/85393294/file.html
I've came across this site: http://unity3dtutorial.com/unity-3d-tutorials/dealing-with-directions-movement-in-unity-3/ Maybe someone will find it useful, however I still can't solve this problem
var mPoss = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
transform.position.x = mPoss.x;
transform.position.y = mPoss.y;
var r = (transform.position - center.transform.position).normalized;
var t = Quaternion.LookRotation(r);
transform.eulerAngles = Vector3(t.x,t.y,t.z);
Here's another try
function Update () {
var MouseWorldPosition = Camera.mainCamera.camera.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0));
MouseWorldPosition.z = 0;
//find the target rotation quaternion
lookRot = Quaternion.LookRotation((MouseWorldPosition - transform.position),Vector3.up);
lookRot.y = 90;
lookRot.z = 270;
//rotate towards the mouse
transform.rotation = lookRot;
}
/* mPoss = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
obj.transform.position = Vector3(mPoss.x,mPoss.y,1.854246);
lookDir = Quaternion.LookRotation(obj.transform.position,transform.position).eulerAngles;
//lookDir.y = 90;
//lookDir.z = 270;
lookDir.x = transform.eulerAngles.x;
transform.rotation = Quaternion.Slerp( transform.rotation, Quaternion.Euler(lookDir),Time.deltaTime*10);
// lookDir = (obj.transform.position - transform.position);
// lookRot = Quaternion.LookRotation(lookDir);
// transform.eulerAngles = Vector3(lookRot.x,lookRot.y,lookRot.z);
// transform.RotateAround(transform.position, Vector3.forward, lookRot.z);
// transform.LookAt(Vector3(transform.position.x,obj.transform.position.y,obj.transform.position.z));
// transform.eulerAngles.z = 270;
//transform.eulerAngles.x += 90;
// transform.eulerAngles.y = 90;*/
Answer by Rynold · Jan 29, 2014 at 05:12 AM
Here's Code I wrote in order to get my cannon to rotate towards where my player clicks on the screen. It will only fire once it reaches its designated angle. Fully commented Hope it helps you out.
if (Input.GetMouseButtonDown(0)) //If the mouse was pressed.
{
centerTarget = new Vector2(0.0f, 7.3f); //Sets center Target to always be straight up in the middle
targetVector = new Vector2(mousePosition.x, mousePosition.y) - new Vector2(transform.position.x, transform.position.y); //Ses the target to be where the mouse is on the screen
newAngle = Mathf.Acos(Vector2.Dot(targetVector, centerTarget) / (targetVector.magnitude * centerTarget.magnitude)) * 180 / Mathf.PI; //Sets the new angle value to be the angle between the center and the new target
if (centerTarget.x < targetVector.x) //If the new target is to the right of the center...
newAngle = -newAngle; //make the angle negative
targetAngle = Mathf.Round(newAngle); //Rounds the new angle to the nearest unit.
targetRotation.eulerAngles = new Vector3(0, 0, targetAngle); //Sets the Target quaternion's euler to a vector with X and Y at 0 and Z at the new angle.
targetObjLocation = Camera.main.ScreenToWorldPoint(new Vector3(e.mousePosition.x, Screen.height - e.mousePosition.y, 7)); //Sets the target objects location to be where the mouse is.
isRotating = true; //Sets isRotating to true to tell the rest of the code its rotating
Instantiate(targetObj, targetObjLocation, Quaternion.Euler(new Vector3(0, 0, 0))); //Instantiates the target object.
}
if (isRotating == true) //If the object is rotating
{
if (transform.rotation != targetRotation) //If it hasn't rotated all the way to where its supposed to yet
{
float step = rotatinSpeed * Time.deltaTime; //Set a step float to the rotating speed * Time.deltaTime
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, step); //Rotate gradually towards the new angle
}
else //If it has rotated all the way to where its supposed to
{
if(coolDown <= 0)
Fire(); //call the fire function
isRotating = false; //Tell the system its not rotating anymore
}
}