Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to rotate towards Z axis

Discussion in 'Scripting' started by Prosmatera, Aug 29, 2016.

  1. Prosmatera

    Prosmatera

    Joined:
    Dec 21, 2015
    Posts:
    89
    Hi guys i have this script
    using UnityEngine;
    using System.Collections;
    [DisallowMultipleComponent]


    public class edrotscript : MonoBehaviour {



    float rotSpeed = 20;

    void OnMouseDrag()


    {
    float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
    float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.Deg2Rad;

    transform.RotateAround(Vector3.up, -rotX);
    transform.RotateAround(Vector3.right, rotY);
    }
    that helps me rotate gameobject towards X and Y axis with mouse drag does someone have idea how i can rotate the gameobject towards Z axis. Thank you
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Well assuming that the Z axis is going into and out of the screen Rotating in the Z-Axis would just "spin in place" so basically the entire world view in front of you would spin around. Like Holding a camera in your hand pointing forward and just spinning it in a circle.

    If thats what your going, you just going to need another set of inputs. In a 3D world mouse can only give you two inputs (up/down and left/right). so you could just pick two keys like Z/X to spin clockwise and counterclockwise. Another option is to dual purpose the Mouse X movement by either seeing if its the left or right mouse button (left would be your normal code, right would be the Z-Axis spin). Finally you also check if ctrl or alt is held down while they have the mouse movement and that woudl change the X-axis to a Z-axis rotation
     
  3. Prosmatera

    Prosmatera

    Joined:
    Dec 21, 2015
    Posts:
    89
    Sorry i didn't quite get that so what im trying to do is rotate the gameobject clock like when i drag the mouse i don't need to be able to rotate it on X and Y i just post the code as example. Can you please edit it in the code. Thank you
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Code (CSharp):
    1. float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
    2. float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.Deg2Rad;
    3.  
    4. if (Input.GetKeyDown(KeyCode.RightControl || Input.GetKeyDown(KeyCode.LeftControl)
    5.     transform.RotateAround(Vector3.forward, -rotX);
    6. else
    7.     transform.RotateAround(Vector3.up, -rotX);
    8. transform.RotateAround(Vector3.right, rotY);
    This is exactly like your code but if the control key is held down it rotates in the z-axis instead.
    if you just need your code to rotate just use Vector3.forward like you'd use up or right, and the angle is however much you want it rotated.