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): #pragma strict var mouse_pos : Vector3; var target : Transform; var object_pos : Vector3; var angle : float; public var isLocal : boolean; function Update () { if (isLocal == true) { mouse_pos = Input.mousePosition; mouse_pos.z = -20; object_pos = Camera.main.WorldToScreenPoint(target.position); mouse_pos.x = mouse_pos.x - object_pos.x; mouse_pos.y = mouse_pos.y - object_pos.y; angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(Vector3(0, 0, angle)); } } Any help is appreciated.
Just change the variable declarations and change function to void. Code (CSharp): private Vector3 mouse_pos; private Transform target; private Vector3 object_pos; private float angle; public bool isLocal; void Update() { if(isLocal) { //rest of the code is the same. } }
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): using UnityEngine; using System.Collections; public class Aim : MonoBehaviour { private Vector3 mouse_pos; public Transform target; private Vector3 object_pos; private float angle; void Update() { mouse_pos = Input.mousePosition; mouse_pos.z = -20; object_pos = Camera.main.WorldToScreenPoint(target.position); mouse_pos.x = mouse_pos.x - object_pos.x; mouse_pos.y = mouse_pos.y - object_pos.y; angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0, 0, angle); } }
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.