- Home /
Selecting objects ingame and rotating it
I have a simple script here in C# that lets me rotate a object (by attaching the script to it) by 90 degrees in either direction which works fine but what i would like to have is to be able to select an object in game and then rotating it by pressing a button. Here is the script i have so far:
using UnityEngine; using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Q)) {
transform.Rotate(0,90,0,Space.World);
}
if(Input.GetKeyDown(KeyCode.E)) {
transform.Rotate(0,-90,0,Space.World);
}
}
}
I'm not a very good programmer so it be greatly appreciated if someone could show me how to select the object and then rotate it with this script. Thanks.
You need to check out raycasting. Program$$anonymous$$g is about learning and being pointed in the right direction. Without making yourself a good programmer, designing games will be non-stop frustration unless you can afford someone to help. :)
I know about raycasting but i dont know enough about it to know how to implement it. I have played around with a piece of raycasting code but couldnt really get it to work. If you could show the structure of the general raycasting code and rotation code together in simple form then i could probably figure out the rest. thanks anyway though.
You are asking for a lot of code. I'll give you advice ins$$anonymous$$d that will be far more valuable. $$anonymous$$aking games is a lot of work, program$$anonymous$$g requires a lot of learning and trial and error, so don't be too hard on yourself. The reason I give this response is because you are going to continually be asking for code, and not getting much help (and even if you do, you're not learning so you'll need specific help again and again). What you really want is to post questions that will lead you to understanding the concepts. For example, what a raycast is. Learning some basics of Vector math to understand it and screen space versus world space for example. There's a lot of prerequisites required to do game program$$anonymous$$g, but it won't take forever. In fact it'll likely take less time then asking for code snippets here and there, and if (more like WHEN) it breaks you'll easily know how to fix it.
Yea i agree with you and i actually have tried to get it to work before posting here. I won't ask for code snippets every single time im stuck I do want to figure it out on my own ( which is actually quite fun once you do figure it out). But for me personally I find I learn better by looking at an example code that works and see how each piece of code works together. But yea I will take your advice and learn the program$$anonymous$$g myself from now on.
I got you. After banging your head several times sometimes just getting the damn code is satisfying. :) Sotirosn's script will be fine. you may want to add a
void Update() {
if (!Input.Get$$anonymous$$ouseButtonDown(0)) { ...Sotirosn's code...} }
Raycasts are expensive (computationally), you can't do too many per frame before performance is impacted. if you limit it to when the mouse button is pressed at least it will take away some time. This is often too much as well depending on the game, so you would want to only cast the ray a few times per second while the mouse button is pressed.. but i'll leave that for you to decide.
Answer by whebert · Jun 14, 2013 at 06:51 PM
The simplest way I can think of to do what you want is to use some of the built in events that Unity automatically sends out for objects with colliders, like OnMouseDown, OnMouseOver, or OnMouseDrag. I don't know the exact behavior you want, but the following would allow you to rotate an object with the mouse and dragging the left mouse button. This will also rotate the object freely, no constraints, relative to your main camera's orientation, so it looks like you are rotating it with the mouse.
using UnityEngine;
using System.Collections;
public class RotateWithMouse : MonoBehaviour {
public float mouseRotateSpeed = 15f;
void OnMouseDrag()
{
transform.rotation = Quaternion.AngleAxis( -Input.GetAxis("Mouse X") * mouseRotateSpeed, Camera.mainCamera.transform.up) *
Quaternion.AngleAxis( Input.GetAxis("Mouse Y") * mouseRotateSpeed, Camera.mainCamera.transform.right) *
transform.rotation;
}
}
And if you really just wanted to rotate with Q and E like in your example, while the object was clicked (with left button), use the following:
void On$$anonymous$$ouseDrag()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q)) {
transform.Rotate(0,10,0,Space.World);
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)) {
transform.Rotate(0,-10,0,Space.World);
}
}
Thanks so much whebert that little piece of code On$$anonymous$$ouseDrag is exactly what i was looking for works very nicely thanks again.
Answer by sotirosn · Jun 14, 2013 at 05:30 AM
Depends how you want to select the object. If you mean doing a 'mouse pick' (taking the 2D mouse cursor, finding its point on the camera lense, making a line from that point straight out from the camera and getting any object that intersects with that line then what you want is,
void Update() {
RaycastHit hit;
if(Physics.Raycast(
camera.ScreenPointToRay(Input.mousePosition), // that line I was talking about
out hit, //information about what you have hit
Mathf.Infinity, // cast through the whole world
0x100 // 0x100 is the layer mask for layer 8, so find any collider in layer 8
)) {
hit.collider.gameObject.AddComponent<RotationScript>();
}
}
btw if you want to rotate something 90 degrees without attaching a script
hit.collider.transform.rotation =* Quaternion.AngleAxis(90, Vector3.up);
Thanks for the code I will try it out when I get home I really appreciate all your peoples advice and help :).
Your answer
Follow this Question
Related Questions
Spinning rigidbody platform in 2D 0 Answers
Bone rotation during animation? 2 Answers
Bike body rotates in opposite direction than wheels 1 Answer
Ball moving only diagonal 0 Answers
How can I rotate an object without moving it up or down? 0 Answers