- Home /
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);
}
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~!
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
![](https://koobas.hobune.stream/wayback/20220613080428im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
About "translating" js into C# 1 Answer
Error CS0246: Type/Namespace Error 3 Answers
using UnityEditor outside of Editor folder when compiling build? 4 Answers
Translate Unity-scripts to uScript 1 Answer
java to C# conversion 1 Answer