How can I rotate something in unity in runtime?
I tried:
transform.rotation = Quaternion.Euler(new Vector3(x,y,z));
,
transform.Rotate(x,y,z);
,
transform.eulerAngles = new Vector3(x,y,z)
Nothing I tried worked.
transform.Rotate (new Vector3 (0, 0, 45) * Time.deltaTime);
Visit this site, it will help you https://learn.unity.com/tutorial/collectable-objects#
sorry if I am wrong
To rotate a GameObject in Unity, the best way is using the Rotate function:
public float speed = 20f
public void Update()
{
transform.Rotate(Vector3.right * speed * Time.deltaTime);
}
With this code, your GameObject will rotate to the right, you can use Vector3.up or Vector3.forward.
If you only want to set a new rotation for your GameObject, change the localEulerAngles of the transform component:
transform.localEulerAngles = new Vector3(x, y, z);
If you want to rotate to a certain position, I recommend using:
transform.rotation = Quaternion.Euler(Vector3.forward * degrees);
Related
I'm trying to Rotate my GameObject using the script around Y axis:
rotateDir = Input.GetAxis("Horizontal");
rb.AddRelativeTorque(Vector3.up * rotateDir * rotateForce);
But my GameObject starting tilt (last picture):
I have changed colliders, checked my code (I can't see any errors)< but still it doesn't work properly. Could anybody help me? thanks
private void Update()
{
RotateTowardsYDirection();
}
public void RotateTowardsYDirection()
{
////transform object to rotate with some rotation factor value
objectToRotate.Rotate(0, Input.GetAxis("Horizontal") * yRotationFactor * Time.deltaTime, 0);
}
There are many ways to rotate gameObject in unity. But the best way to rotate is using EularAngles
// Transform.eulerAngles represents rotation in world space
void Update() {
currentEulerAngles += new Vector3(x, y, z) * Time.deltaTime * rotationSpeed;
//apply the change to the gameObject
transform.eulerAngles = currentEulerAngles;
}
I'm trying to make shooting with a shotgun, I have already made shooting with one bullet. Code:
Vector2 shootingDirection = new Vector2(joystick.Horizontal, joystick.Vertical);
shootingDirection.Normalize();
if (shootingDirection != new Vector2(0, 0))
{
if(isShotGun) ShotGunShoot(shootingDirection);
GameObject bullet = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
bullet.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
bullet.GetComponent<Rigidbody2D>().AddForce(shootingDirection * 10f, ForceMode2D.Impulse);
}
}
But I'm just trying to create two other bullets with a slight deviation from the main one, so that it looks like a fraction, but it does not work correctly. Code:
private void ShotGunShoot(Vector2 dir)
{
GameObject shotGun = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
shotGun.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 30f);
shotGun.GetComponent<Rigidbody2D>().AddForce((dir + new Vector2(-.3f, 0f)) * 10f, ForceMode2D.Impulse);
shotGun = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
shotGun.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + 30f);
shotGun.GetComponent<Rigidbody2D>().AddForce((dir+ new Vector2(.3f, 0f)) * 10f, ForceMode2D.Impulse);
}
As it should be
Here it is now
Thanks for help!
For the rotation. You can simply set the direction (from your image I can see that your bullet has to fly towards its right vector) like e.g.
shotgun.transform.right = dir;
shotgun.transform.Rotate(0, 0, 30);
For the add force: This uses world space directions.
You always pass in the direction and add the additional offset in world space. So that additional offset goes always in X axis direction regardless in which direction you shoot.
Rather take the direction into account by using the local force Rigidbody.AddRelativeForce
shotGun.GetComponent<Rigidbody2D>().AddForceRelative((shotgun.transform.right + new Vector2(0f, 0.3f)).normalized * 10f, ForceMode2D.Impulse);
This now uses the bullets right direction and additionally uses an offset of 0.3 along its local Y axis.
Btw: If you give your prefab the correct type
public Rigidbody bulletPrefab;
you can skip the GetComponent<Rigidbody> calls.
I'm using the InControl input manager for my project. I'm using input from the right stick to rotate my player object, but I want the player to be rotated smoothly rather than instantaneously.
Here's my current code:
void FixedUpdate()
{
var device = InputManager.ActiveDevice;
MoveThePlayer(device.LeftStick.X, device.LeftStick.Y);
RotateThePlayer(device.RightStick.X, device.RightStick.Y);
}
void MoveThePlayer(float movex, float movey)
{
body.velocity = new Vector2(movex * speed, movey * speed);
}
void RotateThePlayer(float movex, float movey)
{
float heading = Mathf.Atan2(movey, movex);
transform.rotation = Quaternion.Euler(0f, 0f, heading * Mathf.Rad2Deg);
}
Lerps are your friend.
transform.Rotate() your player towards the angle you want by using Time.deltaTime
Example:
transform.Rotate(0, 0, (angleIWantToBeAt - transform.eulerAngles.z) * Time.deltaTime * speedMultiplier)
I want to rotate my 3d model based on player touch on x and y direction.
So that I am detecting horizontal and vertical touch.
But in this I want to restrict z direction rotation. For this I tried multiple codes and ask suggestions in other forums too. At present no suggestion is working for me.
Basically, I don't want z direction rotation. Following image give you more idea, where things going wrong. Model rotated completely into z direction. I want to stop this.
Here is my multiple tries to achieve same thing.
void Update ()
{
// If there are two touches on the device...
if (Input.touchCount == 1 && GameManager.Instance.IsGameStart) {
// Store currnet touch.
Touch touch = Input.GetTouch (0);
// transform.RotateAround (transform.position, Vector3.up, - touch.deltaPosition.x Time.deltaTime 15f);
// transform.RotateAround (transform.position, Vector3.right, touch.deltaPosition.y Time.deltaTime 15f);
// transform.RotateAround (transform.position, Vector3.forward, 0f);
// transform.Rotate (Vector3.up, -touch.deltaPosition.x Time.deltaTime 10f, Space.World);
// transform.Rotate (Vector3.right, touch.deltaPosition.y Time.deltaTime 5f, Space.World);
// transform.Rotate (Vector3.forward, 0f, Space.World);
myRigidbody.MoveRotation (myRigidbody.rotation Quaternion.Euler (Vector3.right touch.deltaPosition.y Time.deltaTime 5f));
myRigidbody.MoveRotation (myRigidbody.rotation Quaternion.Euler (Vector3.up -touch.deltaPosition.x Time.deltaTime 10f));
}
}
In above each block represent unique effort to restrict z direction.
Now please give me some suggestion to achieve same thing.
As well my discussion running at Unity forum
Touch based rotation of 3d model
EDIT: I have tried with restricting rigidbody in z rotation. So my inspector look something like this.
EDIT : After discussion on game development chat room. I have following kind of code :
float prevZ = transform.eulerAngles.z;
transform.Rotate (Vector3.up, -touch.deltaPosition.x Time.deltaTime 10f, Space.World);
transform.Rotate (Vector3.right, touch.deltaPosition.y Time.deltaTime 5f, Space.World);
Vector3 modelVec = transform.eulerAngles;
modelVec.z = prevZ;
transform.eulerAngles = modelVec;
I am just near to solution but now my golf globe model can't able to move more that 180 degree from top or bottom side drag. I just reach around 180 degree and it gets just rotated towards any other direction.
Ok after your comments, it sounds like you may need to start fresh.
Based on what I understand, try this out:
Vector3 rotation = new Vector3();
rotation.y = -touch.deltaPosition.x * Time.deltaTime * yRotSpeed;
rotation.x = touch.deltaPosition.y * Time.deltaTime * xRotSpeed;
rotation.z = 0;
transform.Rotate(rotation);
and then I'd suggest adding:
public float yRotSpeed;
public float xRotSpeed;
as fields of your script so that you can adjust the rotating speed within the unity engine in the future.
Just do like this.
// private variables
private Vector3 inputRotation; // difference of input mouse pos and screen mid point
private Vector3 mouseinput;
// Update is called once per frame
void Update() {
FindPlayerInput();
}
void FindPlayerInput()
{
mouseinput = Input.mousePosition;
mouseinput.z = 0; // for no rotation in z direction
inputRotation = mouseinput - new Vector3(Screen.width * 0.5f, Screen.height * 0.5f,0);
ProcessMovement();
}
void ProcessMovement(){
transform.rotation = Quaternion.LookRotation(inputRotation);
}
So I've been trying to get this to work but no luck so far, hopefully you can help. The thing is I have a camera in my project that the user can freely move with mouse and buttons.
Currently like so:
move = new Vector3(0, 0, -1) * moveSpeed;
move = new Vector3(0, 0, 1) * moveSpeed;
...
And then I just add move vector to cameraPos vector: cameraPos += move
Then problem is if I rotate the camera and then try to move, for example down, it will not move straight down but in a certain angle. I am assuming this is due to moving on local axis. But what I want to do is to move on a world axis. Is something like that possible, or do I have to somehow calculate the angle and then move on more than one axis?
Best regards!
EDIT:
I am rotating the camera where cameraPos is the current position of the camera and rotation is the current rotation of the camera. And this is the code to rotate the camera:
void Update()
{
...
if(pressed)
{
int newY = currentY - oldY;
pitch -= rotSpeed * newY;
}
Rotate();
}
void Rotate()
{
rotation = Matrix.CreateRotationX(pitch);
Vector3 transformedReference = Vector3.Transform(cameraPos, rotation);
Vector3 lookAt = cameraPos + transformedReference;
view = Matrix.CreateLookAt(cameraPos, lookAt, Vector3.Up);
oldY = currentY;
}
Ihope this is more readable.
I was able to solve this problem by using:
Vector3 v;
if (state.IsKeyDown(Keys.Up))
v = new Vector3(0, 0, 1) * moveSpeed;
... //Other code for moving down,left,right
if (state.IsKeyDown(Keys.V))
view *= Matrix.CreateRotationX(MathHelper.ToRadians(-5f) * rotSpeed); //Multiplying view Matrix to create rotation
view *= Matrix.CreateTranslation(v); //Multiplying view Matrix to create movement by Vector3 v
I suppose you're already saving the direction you're looking at in a Vector3. Replace your method with this:
direction.Normalize();
var move = direction * moveSpeed;
cameraPos += move;