• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • 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

avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
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;
     }
 }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Prasanna · Jun 23, 2014 at 11:04 AM 0
Share

@ Nerevar, thanks for your code. Its working fine, can you explain me Please?

avatar image meat5000 ♦ · Jun 23, 2014 at 11:16 AM 0
Share

Please accept the answer and give a +1 for his kind efforts :)

avatar image Prasanna · Jun 23, 2014 at 11:17 AM 0
Share

Sorry Forget that...

avatar image Nerevar · Jun 23, 2014 at 11:41 AM 0
Share

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

avatar image Prasanna · Jun 23, 2014 at 12:24 PM 0
Share

Thanks for your that. -Prasanna

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Including a DLL in unity? 1 Answer

A node in a childnode? 1 Answer

Multiple Cars not working 1 Answer

I need help with a script(brauche hilfe mit einen Script) 0 Answers

Unable to check WALL to WALL check for tower defense game 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges