Hello there i saw some movement code in internet, its actually in Java Script i need to convert into C# Script. I tried but some error is there…
Here is Original Code Doesn’t have any errors
var smooth:int;
var clickeffect:GameObject ;
var targetposition:Vector3;
var speed:int;
function Update()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
smooth = 25;
var playerplane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hitdist = 0.0f;
if(playerplane.raycast(ray, hitdist))
{
var targetpoint = ray.getpoint(hitdist);
targetposition = ray.getpoint(hitdist);
var targetrotation = Quaternion.LookRotation(targetpoint - transform.position);
transform.rotation = targetrotation;
}
}
var dir:Vector3 = targetposition - transform.position;
var dist:float = dir.magnitude;
var move:float = speed * Time.deltaTime;
if(dist > move)
{
transform.position += dir.normalized * move;
Instantiate(clickeffect, targetposition, Quaternion.identity);
}
else
{
transform.position = targetposition;
}
transform.position += (targetposition - transform.position).normalized * speed * Time.deltaTime;
}
Here is my Code(after Converted)
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour
{
public int smooth;
public GameObject clickeffect;
private Vector3 targetposition;
public float speed = 60.0f;
void Start()
{
targetposition = transform.position;
}
void Update()
{
if(Input.GetKeyDown (KeyCode.Mouse0))
{
smooth = 25;
Plane playerplane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if(playerplane.Raycast(ray, hitdist))
{
Ray targetpoint = ray.GetPoint(hitdist);
targetposition = ray.GetPoint(hitdist);
Quaternion targetrotation = Quaternion.LookRotation(targetpoint - transform.position);
transform.rotation = targetrotation;
}
}
Vector3 dir = targetposition - transform.position;
float dist = dir.magnitude;
float move = speed * Time.deltaTime;
if(dist > move)
{
transform.position += * move;
Instantiate(clickeffect, targetposition, Quaternion.identity);
}
else
{
transform.position = targetposition;
}
transform.position += (targetposition - transform.position).normalized * speed * time.deltatime;
}
}
-Prasanna