I have a java code but i want to make it into C#. Below is the java:
function Update () {
if (Input.GetMouseButton(0)) {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
hit.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
}
}
}
I have tried outting it to C# but i don’t know why it wont work, can someone help please:
using UnityEngine;
using System.Collections;
public class Movebody : MonoBehaviour {
public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
public RaycastHit hit;
Deleted your hit and ray global variables, they are ignored in your Update function since you declare two new local variables with the same name.
Deleted your local hit variable, since you don’t really use it.
Instead of creating a new Vector3 you can use your ray to place the object 10 units from the camera.
If you want to create a new Vector3 anyway you have to put new in front of Vector3, so new Vector3(..bla..);
using UnityEngine;
using System.Collections;
public class Movebody : MonoBehaviour {
void Update()
{
if(Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, 10))
{
hit.transform.position = ray.GetPoint(10);
}
}
}
}
using UnityEngine;
using System.Collections;
public class testing : MonoBehaviour {
public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
public RaycastHit hit;
public Vector3 mouse_Pos;
void Update()
{
if(Input.GetMouseButton(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, 10))
{
mouse_Pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
hit.transform.position= Camera.main.ScreenToWorldPoint(mouse_Pos);
}
}
}
}
Hey this doesn’t seem to work? I’ve attached the script to the main camera and the camera is in orthographic view, but i am getting three errors still;
ArgumentException: You are not allowed to call INTERNAL_CALL_ScreenPointToRay when declaring a variable.
Move it to the line after without a variable declaration.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:291)
Movebody…ctor ()
UnityEditorInternal.InternalEditorUtility:InspectorWindowDrag(Object[ ], Boolean)
UnityEditor.DockArea:OnGUI()
get_main can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
ArgumentException: get_main can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
Movebody…ctor ()
I have an object that i want to move along the x and z axis not the y, and i want to be able to click on that object and be able to more it anywhere on the screen and when i lift my finger off the click button i want that gameobject to stay where i have moved it.