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

A couple of C# coding questions.

Discussion in 'Scripting' started by cristo, Jan 27, 2017.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi I'm curious about in this line of code,
    Code (CSharp):
    1. transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
    why is
    Code (CSharp):
    1. rotation
    needed after
    Code (CSharp):
    1. transform.rotation,
    ?

    also, is
    Code (CSharp):
    1. if (Input.GetButtonDown())
    the left mouse button by default- the same as
    Code (CSharp):
    1.  if (Input.GetMouseButtonDown(0))
    ?
     
  2. Zukas

    Zukas

    Joined:
    Dec 17, 2013
    Posts:
    40
    In Quaternion.Slerp method, first parameter is the value you will want to change, and second parameter is the value to which you want to Slerp it to. So basically every frame you "Slerp" from transform.rotation (current value) to rotation (target value). It would be best to rename rotation variable to something like targetRotation to avoid confusion for beginners.

    As for second question, no it's not the same. Input.GetButtonDown() is used for Keyboard buttons and GetMouseButtonDown() is used for all moiuse buttons.
     
    Kiwasi likes this.
  3. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Not entirely correct. GetButtonDown is used for getting the input from Unity's input manager, which is works for different controllers. FE. GetButtonDown("Fire1") could be set up as either a keyboard button, mouse button or xBox controller button, etc.
    But yeah, GetMouseButtonDown is indeed only for mouse buttons.
     
  4. Zukas

    Zukas

    Joined:
    Dec 17, 2013
    Posts:
    40
    You're right, my bad. I mistook GetButtonDown for GetKeyDown. GetKeyDown is the one that takes KeyCode enumerator as parameter. For example: Input.GetKeyDown(KeyCode.LeftControl);
     
  5. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Well honestly, while not configurable due to the mentioned enums, GetKeyDown also accepts mouse and controller input.
     
  6. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks for helping clear that up. I think I get it a little better now.:)