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

transform.Rotate() not working

Discussion in 'Scripting' started by Seppi, Jun 23, 2017.

  1. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Move : MonoBehaviour {
    7.  
    8. Rigidbody rb;
    9. public int speed = 10;
    10. public int jumpHeight;
    11. public int rotateSpeed;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.        
    16.         rb = GetComponent<Rigidbody>();
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void FixedUpdate () {
    21.        
    22.         if(Input.GetButtonDown("A")){
    23.  
    24.             transform.Rotate(Vector3.up * -rotateSpeed * Time.deltaTime);
    25.         }
    26.  
    27.         else if(Input.GetButtonDown("D")){
    28.  
    29.             transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
    30.         }
    31.  
    32.         else if(Input.GetButton("W")){
    33.  
    34.             rb.velocity = new Vector3(0,rb.velocity.y,speed);
    35.         }
    36.  
    37.  
    38.         else if(Input.GetButton("S")){
    39.  
    40.             rb.velocity = new Vector3(0,rb.velocity.y,-speed);
    41.         }
    42.  
    43.  
    44.         else if(Input.GetButtonDown("Jump")){
    45.  
    46.             rb.AddForce(Vector3.up * jumpHeight);
    47.         }
    48.  
    49.         else{
    50.  
    51.             rb.velocity = new Vector3(0,rb.velocity.y,0);
    52.         }
    53.     }
    54. }
    55.  
    Currently in the process of switching from JS to C# (not saying that's the problem, maybe I'm just an idiot), and as usual I'm running into problems. The object doesn't rotate with A and D for some reason.

    This is just one problem; a lot of transform.Whatever() methods just refuse to work for me in C#. What am I doing wrong?
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Does rotateSpeed actually have a non-zero value? That is, you set its value in the inspector? By default int values are 0.

    Although I'd actually be surprised if this was the case as I'd have thought Unity would give you warnings/errors in the console about trying to rotate with a zero vector...

    Also, you'll want to use Update() instead of FixedUpdate() since that's really meant for physics components. Using Time.deltaTime inside of FixedUpdate will likely give you odd results.
     
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Time.deltaTime returns Time.fixedDeltaTime when used in FixedUpdate.

    You're using GetButtonDown, which will only return true on the first frame the button is pressed. By your use of Time.deltaTime, I imagine you meant to use GetButton which will return true as long as the button is pressed.
     
  4. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162

    Haha oops. It works now, but I can't turn while moving?
     
  5. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    hint: If... else if....

    I would actually keep your horizontal movements as an if/else if (well, frankly I'd disable rotation if both were pressed, but the choice is yours), and vertical as if/else ifs (or disable if both pressed), but disconnect the horizontal and verticals from each other in your if/else ifs, if you get my meaning. That's the cause of the problem.
     
    Last edited: Jun 24, 2017