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

Converting java to C#

Discussion in 'Scripting' started by Colmonkey, May 24, 2014.

  1. Colmonkey

    Colmonkey

    Joined:
    May 24, 2014
    Posts:
    4
    Hi,

    I am very new to coding. And I have been reading alot, I just havn't actually started coding. So I figured I wasn't learning anything because I wasn't actually doing it. I decided to try to make a small test FPS by converting javascript over to C#.

    So I tried to make a mouse(camera) controller for my FPS. And I have no idea what I did wrong. I'm sure there is plenty wrong... But I thought the best way to learn would be to hear what I did wrong instead of just guessing for hours.

    Here is my code that I am having trouble with:


     
  2. wolvz38

    wolvz38

    Joined:
    Feb 6, 2013
    Posts:
    66
    I think you want some code like this:

    Code (csharp):
    1. h += Input.GetAxis ("Mouse X") * horizontalSensitivity * Time.deltaTime;
    2. v -= Input.GetAxis ("Mouse Y") * verticalSensitivity * Time.deltaTime;
    3.  
    4. v = ClampAngle (v, minVerticalAngle, maxVerticalAngle);
    5.        
    6. newRotation = Quaternion.Euler (v, h, 0.0f);
    7. cameraTransform.rotation = newRotation;
    Also if you want to add smoothing just add a new Quaternion variable and use Quaternion.Slerp function.
     
    Last edited: May 24, 2014
  3. Colmonkey

    Colmonkey

    Joined:
    May 24, 2014
    Posts:
    4

    Hey thanks man, but I guess I wasn't really looking for a new script. I was more so looking for an explanation of what was wrong with my script. I'm only really making this game to learn to code.
     
  4. wolvz38

    wolvz38

    Joined:
    Feb 6, 2013
    Posts:
    66
    Yeah I wasn't trying to give you different code to use. I was just showing you a way it can be done. The camera updates should be in LateUpdate() not Update(). Also you might want to make a new function like the one I have called ClampAngle. It should check if the angle is over 360 and subtract 360 from it, or if it's lower than -360 it should add 360 to it. Then it takes the final angle and clamps it using Mathf.Clamp and returns it.

    Code (csharp):
    1.     public static float ClampAngle(float angle, float min, float max)
    2.     {
    3.         if (angle < -360F)
    4.             angle += 360F;
    5.         if (angle > 360F)
    6.             angle -= 360F;
    7.         return Mathf.Clamp(angle, min, max);
    8.     }