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

2D mouse aiming Js to c#

Discussion in 'Scripting' started by RavenOfCode, Dec 3, 2015.

  1. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    I'm having issues with a 2D shooter Im making. I have a working script in JS and I would like it to be in C# as I could work with it alot better (as I know C#). Could someone convert it for me? I have tried but to no avail.

    Heres the script:
    Code (JavaScript):
    1.   #pragma strict
    2.  
    3. var mouse_pos : Vector3;
    4. var target : Transform;
    5. var object_pos : Vector3;
    6. var angle : float;
    7. public var isLocal : boolean;
    8.  
    9. function Update ()
    10. {
    11.      if (isLocal == true)
    12.      {
    13.      mouse_pos = Input.mousePosition;
    14.      mouse_pos.z = -20;
    15.      object_pos = Camera.main.WorldToScreenPoint(target.position);
    16.      mouse_pos.x = mouse_pos.x - object_pos.x;
    17.      mouse_pos.y = mouse_pos.y - object_pos.y;
    18.      angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    19.      transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
    20.      }
    21. }
    22.  
    Any help is appreciated. :)
     
  2. DMSolace

    DMSolace

    Joined:
    Nov 30, 2015
    Posts:
    9
    Just change the variable declarations and change function to void.

    Code (CSharp):
    1. private Vector3 mouse_pos;
    2. private Transform target;
    3. private Vector3 object_pos;
    4. private float angle;
    5. public bool isLocal;
    6.  
    7.     void Update()
    8.     {
    9.         if(isLocal)
    10.         {
    11.             //rest of the code is the same.
    12.         }
    13.  
    14.     }
     
    RavenOfCode likes this.
  3. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Thanks! :)
    There was an error with that (transform.rotation= Quaternion.Euler(Vector3(0,0, angle)); should be transform.rotation= Quaternion.Euler (0,0, angle);) but that was an easy fix. Thanks again.

    For anyone who needs a 2D mouse aiming script, this works:
    Target is the object you want to aim,

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Aim : MonoBehaviour {
    5.      
    6.     private Vector3 mouse_pos;
    7.     public Transform target;
    8.     private Vector3 object_pos;
    9.     private float angle;
    10.  
    11.     void Update()
    12.     {
    13.             mouse_pos = Input.mousePosition;
    14.             mouse_pos.z = -20;
    15.             object_pos = Camera.main.WorldToScreenPoint(target.position);
    16.             mouse_pos.x = mouse_pos.x - object_pos.x;
    17.             mouse_pos.y = mouse_pos.y - object_pos.y;
    18.             angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    19.             transform.rotation = Quaternion.Euler(0, 0, angle);
    20.     }
    21. }
    22.  
     
  4. ArtificialElements

    ArtificialElements

    Joined:
    Jun 1, 2020
    Posts:
    2
    Yes! Perfect! Thank both of you! It works the way I needed it too! there is sadly a scarce amount of information on this in the forum.