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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotate and obejct with Key

Discussion in 'Scripting' started by LegacyOfWax, May 9, 2020.

  1. LegacyOfWax

    LegacyOfWax

    Joined:
    Jun 12, 2014
    Posts:
    13
    looking to try and rotate and object in one axis with a key press and am not able to wrap my head around how this works.

    What I am trying to do. have it so the player hits the r key and the object rotates in the Yaxis 90%.

    I had this working at one point but it wasn't smooth. so i looked into making a smooth motion (through a jason Weimenn
    ) I understand how that works.

    But what I think I need to be able to do is get just the X and Z axis of the object have it stay put and then move it 90 on the y. but i cant figure out how to extract and axis rotation.

    any help would be appreciated thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Tile : MonoBehaviour
    6. {
    7.     private Transform startRotPos;
    8.     private Transform endRotPos;
    9.  
    10.     private float rotAmount = 90f;
    11.  
    12.     [SerializeField] [Range(0f, 1f)]
    13.     float lerpPct;
    14.  
    15.     void Update()
    16.     {
    17.         endRotPos.rotation = Quaternion.Euler(Input.GetAxis("startRotXPos"), rotAmount, Input.GetAxis("startRotZPos"));
    18.  
    19.         if (Input.GetKeyDown("r"))
    20.         {
    21.             transform.position = Vector3.Lerp(startRotPos.position, endRotPos.position, lerpPct);
    22.             transform.rotation = Quaternion.Lerp(startRotPos.rotation, endRotPos.rotation, lerpPct);
    23.  
    24.         }
    25.     }
    26. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    The best way to precisely control only one axis of rotation is to put extra GameObjects into your hierarchy, and then each one pivots only one axis, and you directly control it.

    For instance, the deck gun on a battleship might look like this:

    Screen Shot 2020-05-08 at 8.22.22 PM.png

    Once you have a barebones setup like the above and scripts to drive it, then you can go back and "hang" any random geometry on any of the given parts: the turret, the barrels, etc.
     
  3. LegacyOfWax

    LegacyOfWax

    Joined:
    Jun 12, 2014
    Posts:
    13
    Thanks for the reply. I'm not sure what you mean by offset of y in your example?

    Also I am having issues understanding when to use rotate rotation get axis or any of these. When I ghink I have an idea and try it i get a "blah blah can't be used as a method". So I bel8ve i need to know how to get just one axisbof an object?