Hi everyone,
I would like to realize the following small program:
Here is the code:
public class NewBehaviourScript : MonoBehaviour
{
Grid theGrid;
Transform cachedTransform;
void Awake()
{
cachedTransform = transform;
if (theGrid)
{
theGrid.AlignTransform(cachedTransform);
cachedTransform.position = CalculateOffsetZ();
}
}
void OnMouseDrag()
{
Vector3 CursorScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
CursorWorldPoint BoolVector3 = Camera.main.ScreenToWorldPoint(cursorScreenPoint);
cachedTransform.position.x = CursorWorldPoint.x;
cachedTransform.position.y = CursorWorldPoint.y;
theGrid.AlignTransform(cachedTransform);
cachedTransform.position.x = CalculateOffsetZ();
}
void CalculateOffsetZ();
{
int gridPosition = theGrid.WorldToGrid(chacedTransform.position);
gridPosition.x = -0.5 * cachedTransform * lossyScale.z;
return theGrid.GridToWorld(gridposition);
}
The problem is that the code is in Java Script and not in C#.
If someone knows JavaScript,
it would be nice to give me a hand.
Thanks to you,
A+
The above code is C#.
However you’re missing common using statements and you have not renamed your class away from NewBehaviourScript.
If you have errors, remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
The complete error message contains everything you need to know to fix the error yourself.
Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors.
The important parts of the error message are:
- the description of the error itself (google this; you are NEVER the first one!)
- the file it occurred in (critical!)
- the line number and character position (the two numbers in parentheses)
- also possibly useful is the stack trace (all the lines of text in the lower console window)
All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.
The code youo provided appears to be be in C#, not JavaScript.
Off the cuff I just see one issue which is an extraneous semicolon which should be removed at the end of line 30. Aside from that, the compiler will tell you any other issues.
Oh - also you’re missing using
directives.
Okay, now I see, the video uses Javascript. That marks it as uselessly old. Throw it out and go get a new video made within the last 5 or less years. Snapping something is like the most trivial thing in the world, not worth cross-porting something from so long ago.
If you have a value A
and you want to snap it to the nearest B
, the general expression is:
var SnappedA = Mathf.Round( A / B) * B;
That’s it.
1 Like
En fait, le code est en Java Script, mais j’ai réussi à traduire certaines parties du code.
Le vrai code Java Script est le suivant :
#pragma strict
var theGrid Grid ;
var cachedTransform Transform;
function Awake ( )
{
cachedTransform = transform ;
if ( theGrid )
{
theGrid. AlignTransform ( cachedTransform ) ;
cachedTransform. position = CalculateOffsetZ ( ) ;
}
}
function OnMouseDrag( )
{
var CursorScreenPoint : Vector3 = new Vector3 ( Input. mousePosition . x , Input. mousePosition . y , 0 ) ;
var CursorWorldPoint : Vector3 = MainCamera . ScreenToWorldPoint ( cursorScreenPoint ) ;
cachedTransform. position. x = CursorWorldPoint. x ;
cachedTransform. position. y = CursorWorldPoint. y ;
theGrid. AlignTransform ( cachedTransform ) ;
cachedTransform. position. x = CalculateOffsetZ( ) ;
}
function CalculateOffsetZ ( );
{
var GridPosition = theGrid. WorldToGrid ( chacedTransform. position ) ;
GridPosition. x = - 0,5 * cachedTransform * lossyScale. z ;
return theGrid.GridToWorld (GridPosition) ;
}
Je vous serais très reconnaissant si vous pouviez m’aider à le traduire en entier.
Merci encore,
A+