how do i rotate an object? C#
hello, i'm new to unity and i'm trying my best to get things work fine. after watching some tutorials i ended up having this code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class cmovement : MonoBehaviour {
public KeyCode PressUp;
public KeyCode PressDown;
public KeyCode PressLeft;
public KeyCode PressRight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (PressUp))
transform.Rotate(Vector3.right, Time.deltaTime);
}
}
so i assigned the W key to the variable PressUp, aaaaand it doesn't work. i wonder why
Answer by g__l · Jul 04, 2017 at 12:00 PM
Try this
using UnityEngine;
using System.Collections;
public class RotateObject : MonoBehaviour {
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Rotate(Vector3.right);
}
}
}
Check out keycodes and different types of input to gain greater understanding and avoid future mistakes, if you have any questions feel free to ask
Input - https://docs.unity3d.com/ScriptReference/Input.html Keycodes - https://docs.unity3d.com/ScriptReference/KeyCode.html
Your answer
Follow this Question
Related Questions
my camera is rotating on Z axis when i just set it on X and Y 0 Answers
how do I rezolve "look rotation viewing vector is zero"? 1 Answer
How to limit only one axis rotation? c# 1 Answer
How to Destroy game object horizontally and vertically , when hit by a Raycast 0 Answers
Rotating Object Smoothly Exactly 90 degrees on click (for infinite rotations) 0 Answers