- Home /
Question by
Bashlet · Apr 02, 2017 at 10:09 AM ·
rotationbeginnermouseclick
Trying to make rotation stay in top down 2D on mouse button release
So I'm a complete beginner to both Unity and C#. I am working on movement mechanics and I want to be able to rotate the player sprite with the mouse and have it stay in the chosen position upon releasing the mouse. Currently, the script I have written is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerRotation : MonoBehaviour {
public float speed;
void Update()
{
//Warps "shape" of player sprite through rotation
if (Input.GetMouseButton (0)) {
Debug.Log ("Held");
var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Quaternion rot = Quaternion.LookRotation (transform.position - mousePosition, Vector3.forward);
transform.rotation = rot;
transform.eulerAngles = new Vector3 (transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z);
GetComponent<Rigidbody2D> ().angularVelocity = 1;
}
else {
//Do Nothing
}
}
}
Any Help would be appreciated.
Comment
Your answer
Follow this Question
Related Questions
Simple Rotation 1 Answer
Rotate UI Image cirlce around self OnMouseclick 1 Answer
Object's rotation is messed up whenever it's x reaches 90 1 Answer
Beginner question 1 Answer
simple movement and rotate help! 1 Answer