- Home /
This question was
closed Jun 23, 2014 at 11:29 AM by
meat5000 for the following reason:
The question is answered, right answer was accepted
Question by
Prasanna · Jun 23, 2014 at 09:56 AM ·
c#javascriptconverting
Converting Code Problem
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
Comment
Best Answer
Answer by Nerevar · Jun 23, 2014 at 10:06 AM
here : 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, out hitdist))
{
Vector3 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 += dir.normalized * move;
Instantiate(clickeffect, targetposition, Quaternion.identity);
}
else
{
transform.position = targetposition;
}
transform.position += (targetposition - transform.position).normalized * speed * Time.deltaTime;
}
}
@ Nerevar, thanks for your code. Its working fine, can you explain me Please?
Please accept the answer and give a +1 for his kind efforts :)
You declared a wrong type variable line 29 You had forgotten to write dir.normalized at line 34 and you miss orthographed Time.deltaTime at the end
I just corrected regarding the code errors I saw in the console :p