I am rather new to scripting but I have been learning a lot. After watching a tutorial I have an awesome touch control script, but I am trying to convert it over from JavaScript to C#.
Here is what I have in JavaScript:
#pragma strict
public var gravityDown : float;
private var ray : Ray;
private var hit : RaycastHit;
function Update ()
{
if(Input.GetMouseButtonUp(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,hit))
{
transform.position.x = hit.point.x;
transform.position.y = hit.point.y;
this.GetComponent.<Rigidbody2D>().gravityScale = gravityDown;
}
}
}
Here is my attempt at re-writing it in C#:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
//gravity multiplier
public float gravityDown;
//used for raycasting
public Ray ray;
public RaycastHit hit;
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonUp (0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,hit))
{
transform.position.x = hit.point.x;
transform.position.y = hit.point.y;
this.rigidbody2D.gravityScale = gravityDown;
}
}
}
}
I get the following errors:
Assets/Scripts/PlayerMovement.cs(21,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit)’ has some invalid arguments
Assets/Scripts/PlayerMovement.cs(23,43): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position’. Consider storing the value in a temporary variable
Assets/Scripts/PlayerMovement.cs(21,36): error CS1620: Argument #2' is missing out’ modifier
Assets/Scripts/PlayerMovement.cs(21,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit)’ has some invalid arguments
I have been pretty stuck on this, but I feel like it is very close. The JavaScript runs perfectly and all I am doing is trying to re-write it in C#. If someone could point me in the right direction that would be great!