How to tilt a sprite based on pointer position?
I have been trying to piece together parts of this C# script but as I am still a beginner I cannot seem to get this to work.
I have a "beam" Sprite (like an iron girder) that the user moves up/down & left/right with the mouse when the click and drag on it. So far so good. I know the Rect of the Sprite, I know where the mouse click is in the Sprite BUT what I need to do now is allow the player to tilt the beam if they move the mouse to one end of the beam or the other while dragging.
So if the user grabs the beam in the center and starts dragging it left, it moves left, but if they start pulling the mouse along the left side of the beam and down, I need the beam to tilt down.
Answer by AarshSinghVishen · Jun 17, 2017 at 02:19 AM
First off you need to get the mouse position using Input.GetAxis("Mouse X") for the x axis and Input.GetAxis("Mouse Y") for the y axis. Now if you want to check if the mouse has moved to left you say: if( Input.GetAxis("Mouse X")>0 ){// Insert Code here.} If you were to save these values in a float variable called xpos, you can rotate the sprite as such :
transform.Rotate(new Vector3(xpos,0,0)); // Note: This will rotate the sprite in the x plane only, you can just change it to suite your needs. "I am still a beginner I cannot seem to get this to work. " I've been there and I know it's frustrating but just keep at it. It gets fun I promise ! Hope I helped and have fun!