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

Converting javascript to c#

Discussion in 'Scripting' started by markblank05, Nov 13, 2015.

  1. markblank05

    markblank05

    Joined:
    Apr 2, 2015
    Posts:
    29
    Hi, I found this script around here about third person control and when I try to convert it to c# it's not working.

    Code (CSharp):
    1. var moveVectorX : Vector3 = theCamera.forward * inputY;
    2. var moveVectorY : Vector3 = theCamera.right * inputX;
    3. var moveVector : Vector3 = ( moveVectorX + moveVectorY ).normalized * movementSpeed * Time.deltaTime;
    4.      
    5. myTransform.position = myTransform.position + Vector3( moveVector.x, 0.0, moveVector.z );
    6.      
    7. myTransform.LookAt( myTransform.position + Vector3( moveVector.x, 0.0, moveVector.z ) );
    and this is what i come up with.

    Code (CSharp):
    1.  Vector3 moveVectorX = mainCamera.forward * inputY;
    2. Vector3 moveVectorY = mainCamera.right * inputX;
    3. Vector3 moveVector = (moveVectorX + moveVectorY).normalized * moveSpeed * Time.deltaTime;
    4. transform.position = transform.position + Vector3 (moveVector.x, 0f, moveVector.z);
    5. transform.LookAt (transform.position + Vector3 (moveVector.x, 0f, moveVector.z));
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    lines 4 and 5 need the 'new' keyword for the vector3s
    .... + new Vector3 ( ... ) ;
     
  3. markblank05

    markblank05

    Joined:
    Apr 2, 2015
    Posts:
    29