• 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 /
avatar image
0
Question by Kona · Oct 22, 2010 at 09:07 AM · javascripttranslatenamespacecs0246

CS0246: The type or namespace name `targetPosition' could not be found. Are you missing a using directive or an assembly reference?

I found a js on the wiki that allow objects to move to the point where the mouse is clicked, like in rpg games. Trying to convert it to C# now but I've been stuck at the same problem for about an hour now and feel like I need some help, I'ld really appreciate it. :)

Here's the cs script at this point, (I add the js at the bottom aswell so you can have a look at it, since the js one is fully functional.) using UnityEngine; using System.Collections;

public class Controller : MonoBehaviour { public int speed = 3; private int targetPosition = Vector3;

void Update () { if(Input.GetKeyDown(KeyCode.Mouse0)){ playerPlane = new Plane(Vector3.up, transform.position); ray = Camera.main.ScreenPointToRay (Input.mousePosition); hitdist = 0.0;

     if (playerPlane.Raycast (ray, hitdist)) {
         targetPoint = ray.GetPoint(hitdist);
         targetPosition = ray.GetPoint(hitdist);

         targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
         transform.rotation = targetRotation;
     }
 }
 MoveTowards(targetPosition);

}

void MoveTowards (position Vector3) { direction = position - transform.position;
direction.y = 0;

 if (direction.magnitude < 0.5) {
     SendMessage("SetSpeed", 0.0);
     return;
 }

 // Modify speed so we slow down when we are not facing the target
 forward = transform.TransformDirection(Vector3.forward);
 speedModifier = Vector3.Dot(forward, direction.normalized);
 speedModifier = Mathf.Clamp01(speedModifier);

 // Move the character
 direction = forward * speed * speedModifier;
 GetComponent (CharacterController).SimpleMove(direction);

 SendMessage("SetSpeed", speed * speedModifier, SendMessageOptions.DontRequireReceiver);

}

}

<// JS VERSION BELOW*\*> var speed = 3;

private var targetPosition:Vector3;

function Update () { if(Input.GetKeyDown(KeyCode.Mouse0)) { var playerPlane = new Plane(Vector3.up, transform.position); var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hitdist = 0.0;

if (playerPlane.Raycast (ray, hitdist)) { var targetPoint = ray.GetPoint(hitdist); targetPosition = ray.GetPoint(hitdist); var targetRotation = Quaternion.LookRotation(targetPoint - transform.position); transform.rotation = targetRotation; }

} MoveTowards(targetPosition);

}

function MoveTowards (position : Vector3) { var direction = position - transform.position; direction.y = 0; if (direction.magnitude < 0.5) { SendMessage("SetSpeed", 0.0); return; }

// Rotate towards the target

// Modify speed so we slow down when we are not facing the target var forward = transform.TransformDirection(Vector3.forward); var speedModifier = Vector3.Dot(forward, direction.normalized); speedModifier = Mathf.Clamp01(speedModifier);

// Move the character direction = forward speed speedModifier; GetComponent (CharacterController).SimpleMove(direction);

SendMessage("SetSpeed", speed * speedModifier, SendMessageOptions.DontRequireReceiver);

}

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

· Add your reply
  • Sort: 
avatar image
0

Answer by zannghast · Oct 22, 2010 at 09:36 AM

Is the C# version you posted the complete one? I can't see where you declared 'targetPosition' and a bunch of other variables.

Cheers~!

Comment
Add comment · Show 1 · 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 Kona · Oct 22, 2010 at 10:40 AM 0
Share

It's the complete one, did something weird when creating the thread so the code-windows got messed up, sorry about that. Anyways before the Update function you can find public int speed = 3; private int targetPosition = Vector3; I somehow got the "targetPosition" related error fixed, dunno how though since I've gotten myself so confused by this point. However there seem to be another namespace that cant be found now called "position", this I know isn't declared, but looking through the original JS one I can't find it declared there either and yet that script work, not sure how to declare it

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

No one has followed this question yet.

Related Questions

Error CS0246: Type/Namespace Error 3 Answers

About "translating" js into C# 1 Answer

using UnityEditor outside of Editor folder when compiling build? 4 Answers

Translate Unity-scripts to uScript 1 Answer

java to C# conversion 1 Answer


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