I'm developing a 3D game for Android platform in a first person style. During one part into the game, the player clicks an object (Item), and the object should be rotated in 45 degrees.
The problem begins when by clicking the object, I can see in the log console the angles and the values have changed, but the player can't see in the game itself the object (In this matter is a form object, like square) has rotated.
I have tried several ways to make the object rotate inside the game, so the player could see it, but without success.
Here is the relevant block of code (One of my tries to solve the problem):
if (array[i].name == "Quad")
{
Debug.Log("Quad object hitted");
Quaternion rotation_value = hit.transform.gameObject.GetComponent<Transform>().rotation;
Vector3 angle = rotation_value.eulerAngles;
Debug.Log(angle);
Vector3 rot = new Vector3(0, 90, (angle.z + 45));
Quaternion rot2;
rot2.eulerAngles = rot;
hit.transform.gameObject.GetComponent<Transform>().rotation = rot2;
}
Any help / suggestion of alternative code in order to solve it will be helpful.
Per the Unity documentation on Transforms:
Do not attempt to edit/modify rotation.
Instead use the Rotate() method for a Transform component. It will modify the Transforms local rotation.
For example:
hit.transform.Rotate(0f, 45f, 0f);
will add a rotation of 45 degrees on the Y-axis every time it's called.
Fixing your code to make this work would look similar to:
if (array[i].name == "Quad")
{
hit.transform.Rotate(0f, 0f, 45f);
}
The Unity documentation for further reading: https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
Related
visualization of what I want
Hi, I am new at unity and I want to rotate edge of my enemy object but I googled a lot and the only algos that I find is about rotating whole object. Like;
obj.transform.rotation = Quaternion.Euler(0.0f, Mathf.PingPong(Time.time * 30f, 90.0f), 0.0f);
it rotates whole object of course, but I dont know how to focus on just an edge. I just want to rotate it like a door opening
One way to do this would be to do transform.rotateAround. You can specify which axis and the amount of degrees. Here is the link to the docs: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html
I am looking at developing a game within Unity by using C#
The game will take the gyroscope orientation and rotate accordingly to the direction of which the phone is rotated.
I am curious of how this would work, I understand how I would be able to read and update the gyroscope orientation however I am unsure on how to assign this to a world to rotate. There will be a player on world which will be the next challenge to prevent the player clipping through the world when it is rotated.
Hence the world should rotate around the players current location.
I currently have no code as I am in the process of designing this, however i am unable to get the logic of how to make this work within my head
Thankyou
If I get your objective right, I guess rotating the camera like in a Fist-Person game should be enough, and would be way simpler than rotating the whole world while keeping the player static.
This is how I would go about it, first I wouldn't rotate the world, as that is alot of objects to rotate, but if you really wanted to you could parent all of the world to a single game object, then rotate that object about an axis based off of the players position(See this: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html).
The simplest way is to attach your main camera to your player(Drag and drop it on your Player Object in your hierchy), then rotate your player.
for an example you can test on your machine without the gyroscrope:
using UnityEngine;
public class rotatorScript: MonoBehaviour {
void Update() {
// Will Rotate based off of left/right arrows
float rotator = -Input.GetAxisRaw("Horizontal");
// Move up or down based off of up/down Arrows
float verticalDirection = Input.GetAxisRaw("Vertical");
// Do the actual movement... using space.self so it is based on the player not the world.
transform.Translate(Vector3.up * verticalDirection * Time.deltaTime * 5f, Space.self);
transform.Rotate(0f,0f,90f * Time.deltaTime * rotator);
}
}
This script would be attached to the player.
in unity editor, when i enable "Pivot", gameobject will rotate around "pivot point" position, when i enable "Center", gameobject will rotate around "center point"
but if i use script to rotate, it always rotate around "center point", for ex, here is my scene:
I use following code:
void Start()
{
// transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
transform.RotateAround(transform.position, new Vector3(0, 0, 1), 90);
}
it rotate around "center point"
if object has collider, i can get pivot point with colider.bounds, if not, how should i do?
and even worse, in some case, i hope rotate to like set rotate in unity editor inspector, i use following code:
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90))
I have no idea to adjust above code to rotate around pivot point
update
I think if i can "rotate", i can also impl "rotate to", so the key point is, how should i get "pivot point" that is used in RotateAround, to object has collider, I can get with colider.bounds with transform.position, to no collider object, how should i do?
The pivot point is the-same as the transform.position you're already using. If you don't like where the pivot point is or the location the rotation is rotating against, you have two options:
1. Create new empty GameObject. Move it to the position you want your GameObject to rotate around. Once satisfied by that location, make this new GameObject a child of the GameObject you will be rotating around. Use the position of this new GameObject as the pivot rotation point for the transform.RotateAround function.
Drag the empty GameObject to the customPivot slot in the script below. That should give you a new pivot point to rotate your GameObject around.
public Transform customPivot;
void Update()
{
transform.RotateAround(customPivot.position, Vector3.up, 20 * Time.deltaTime);
}
2. Open a 3D application and change the pivot point of your object, then save and re-import it into Unity.
Since this is just used to rotate object around, I suggest #1. If the problem is that the pivot point is not centered, I would suggest #2.
so the thing is: i have a sensor that makes a rigidbody rotate, the problem is that i can't calculate the speed in which it was rotated (If the sensor was moved fast the object would also move fast but when it collides with another gameobject it won't apply the speed/force which with it has been rotated)
Is there any way I can calculate the speed/force in which it has recently rotated so that i can apply that speed/force to another object when it collides?
note: im not entirely sure if what i need is speed or force
float rot = float.Parse (sp.ReadLine());
transform.eulerAngles = new Vector3 (0, 90, rot);
note: because of how the object was designed it has to be rotated by default in 90 degrees. Basically what im doing is receiving information from a Gyroscope and puting it in the object rotation.
The sensor gives values from -90 to 90 in the form of Float, when it is rotated to the left it gives values from -90 to 0, when rotated to the right it gives values from 0 to 90. this values should be used to graphically rotate the object and also aply the speed in which it was rotated when it collides whith another gameobject (if it was moved fast from -90 to -20 for example it should apply more force than if it was moved slowly from -90 to -50 to -20)
When trying to cause believable physical interaction between objects, you never want to manipulate the transform directly. Instead, you should be making use of the various methods available to physically influence an attached Rigidbody.
To apply rotation in this case, you're going to want to use Rigidbody.AddTorque(). Here's an idea of how you might use it:
// Probably get this value in Start() and save it for later
Rigidbody rb = GetComponent<Rigidbody>();
float rot = float.Parse (sp.ReadLine());
// transform.forward is the unit vector corresponding to the local z-axis of the object
Vector3 rotationAxis = transform.forward * rot;
rb.AddTorque(rotationAxis);
Hope this helps! Let me know if you have any questions.
I'm trying to rotate an object based on mouse cursor movement(like bubble gun in bubble shooter game). But I'm Filed to do that with my script.
MY script is :
mouse_pos = Input.mousePosition;
Debug.Log(mouse_pos);
object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0,-angle);
It rotates only its same position it won't move.
Please any one help me to solve this. Or point me to any good tutorial ....
If I understand your problem correctly, then the object's transform is rotating around it's local origin, which is why you are seeing the correct rotation but no translation.
Unity applies transformations in the following order: scale, translate, rotate.
The easiest way to solve the problem is to parent your object's transform to another parent transform and rotate the parent transform instead (having applied a translation to your object's transform). Note that you will have to modify any scaling or translation on your object's transform because it will now be inheriting the parent transform's rotation.
Hope that helps, otherwise please provide more detail on your problem.