- Home /
Rotate Object 180º OnClick
Hello, Im having a little trouble im making a Memory Match Game. Im new at Unity and C# and I dont know how to make this script.
When the player click on the object (card) it should rotate 180º to see the otherside.
Someone can help me with this?
So, where exactly is the issue? The more precisely you formulate your question, the better we can help you. Are you having trouble with the rotation part, checking when the player clicked on a card etc. etc.
Answer by NikunjPopat · Jul 02, 2014 at 02:09 PM
Apply code snippet to GameObject
//C#
void OnMouseDown(){
transform.Rotate(0,0,180);
}
//java script
function OnMouseDown(){
transform.Rotate(0,0,180);
}
Answer by Hikaros · Jul 01, 2014 at 11:45 PM
There are two options (that i know of)
void OnMouseDown(){
transform.rotation = Quaternion.Euler(0,0, -90);
}
and
void Update() {
if (Input.GetMouseButtonDown(0)){
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit){
transform.rotation = Quaternion.Euler(0,0, -90);
}
}
}
-90 rotates to the right, 90 to the left.
EDIT:
i re read question lol, i thought u wanted to rotate it sideways. do you want to flip it?
If that is the case i don't think you can do that with a 2d sprite (because it is the same back and front so if you "rotated" it 180 degs you'd have the same image but mirrowed), however you can change sprites (one being front and one being the back of it). To change the sprite should be something like:
public SpriteRenderer jackRenderer;
public Sprite backOfCard;
void OnMouseDown(){
GetComponent<SpriteRenderer>().sprite = backOfCard;
}
Answer by YoungDeveloper · Jul 01, 2014 at 11:46 PM
Unity has great tutorials related to rotation and movement.
i think he wants to flip it to show the front/back of a card but in 2D (if i'm not wrong) if you "rotate" the card 180 degrees you will have the same image but mirrowed haha so changing the sprite should be the way to go or so i think x_x
Dont know, he never mentioned nothing about 2D though.
lol you are right, i completely assumed that myself hahaha it is just so weird to see a 3D card game (at least in my case) so i assumed it, my bad o:
Your answer
Follow this Question
Related Questions
Reference creator of gameObject 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How can I move a player toward a gameobject by clickling an image 0 Answers
Rotate gameobject on Y axis 1 Answer