Hi,
I have this script, just a basic movement along the planes. I have noticed something interesting in Unity, and it seems all objects that move are subject to physics and "real world (I use that term loosely), physics. So because of that, I have created a behaviour which removes all drag if btn down, if up, increase drag.
The curious thing about how this behaviour is behaving is that …
a. the object often just stops, with out calling the halt(). I am just holding the directional button and it just stops. Why? Any reason you can see?
b. Sometimes the object will just shoot off at a very high velocity. Mainly if I “tap, tap TAP!” My logic is that velocity is additive. If this is the case, is there a way to just have an object move a X vel all the time, if it is moving? Think Mega Man, or Pac Man etc. one velocity with pretty much immediate stops. Its not physics but is cool for games.
However, based on your examples (Mega Man, Pac Man), you don’t want rigidbody physics. You want Lerp Controllers. The oldschool platformer movement was never meant to be physics derivative (MegaMan can control his direction mid-air, Pacman comes to a full stop as soon as you turn around, there’s almost never acceleration / deceleration and drag is almost never applied).
Physics are for racing games and simulators mostly. For platformers or FPS, you usually go for Lerp. Unity gives you a head-start on this with the CharacterController class.
Hi
I am just trying to write some characterController scripts myself to better understand them. I am looking thru the scripts of examples from reference but they seem very confusing. Am I waisting my time? Does the set up Unity has provided cover all my bases?
I don’t know if I mentioned this before to you, but I address most of your issues (classes, data structures, controllers, etc) on my online youtube tutorials. You should check them out. Link is on my signature.
On this subject, I create a hybrid physics / translate character controller in my tutorial project. It should give you some pointers.
As for whether or not you’re wasting your time, it depends on what kind of controls you want for your character. Unity’s default CharacterController class works wonders for FPS games, but as we’ve said a racing game is going to require proper physics to operate and CharacterController won’t cut it. Each game demands its own unique controls, and it’s up to you whether you’re going to create your own or use the standard defaults.
Just to let you know, everything seems to be going out the window as far s what I know. Does it really change it up so much? I mean just not having a rigidbody to associate with seems to cause a bit of problems.
I tried making this cameraScroll.js code to follow the player… but I keep getting errors. Anyhow, not too worry and I dont want to bitch and complain, just a bit thrown off with the new option.
var target : Transform;
var zOffset : float;
function Update(){
var targetPos = target.position;
print("targetPos " + targetPos);
zOffset = targetPos.z + zOffset;
transform.position = (targetPos.x, targetPos.y, zOffset);
}
and I would spot it faster if you posted the errors themselves as I’ve asked you in the past
In order to assign values to a Vector struct, you need to construct a new one first and then assign it to the current one. In short it should be like this :
That said, your entire code would be much simpler if it were like this :
var target : Transform;
var zOffset : float;
function Update(){
transform.position = target.position + Vector3(0,0,zOffset);
}
I have no idea what this code is trying to achieve tho…
EDIT : Nevermind, I got it, it’s your attempt to make the camera follow the player. Okay, yeah, this would work. You can also use TransformPoint() which is Unity’s way of doing what I suggested with even less calculations. It would be something like
var target : Transform;
var zOffset : float;
function Update(){
transform.position = target.TransformPoint(0,0,zOffset);
}
It does exactly the same thing, takes the target’s position and adds the 3 arguments to its Vector3.
Thanks Diviner,
What I am trying to figure out now is…
if my player object, is not a rigidbody, and I want to move it from a to be, in a linear line. how do I adress it in script?
First of all I am unclear on what a coroutine is. It is a way for multiple functions to be called at once? As it pipelines the commands? Sorry but I am unsure… having said that tho… it looks to me that he is using the script title as a pointer. Am I correct. This bit has me confused. Maybe it is late.
:?
Specifically, moveType. I looked it up, in reference but there is no such article. Is this a class he has created all his own? The trouble for me is that he already has a predefined method as to what it does with its own variables, time and speed. Is this just great programming?? Again, sorry if my questions seem unthought out, it is just what strikes me here.
Also, MoveObject, is used as a pointer to the object to be moved, but I do not see it declared as a variable, prior to Start(). It appears that it is a class tho.
hmm, there is a lot I am going to over this in the morning.
A co-routine is pretty much the same as a routine, as a function, as a method and so on. People come to Unity from different scripting backgrounds and each one carries his own terminology with him when he comes. In Java we call routines methods, in Fortran we call them routines, in Javascript functions and so on. They’re all pretty much the same. Usually a co-routine is a routine called from inside a different routine. If Start() is called a routine, all functions you call from inside Start() are called co-routines. But they’re all the same, I wouldn’t worry about terminology if I were you.
As for the Script title.
When I explained variable types to you, I mentioned Static variables, which you call by first invoking the name of the script that declared them and then the name of the variable like
ScriptThatDeclaresVariable.variableName = x;
And I also mentioned to you that functions are also usually declared as variables, and therefore a function can be static. For example if we want to run a static function called Translation from the script that declares it called MoveObject, first we declare that function like this :
static function Translation ( //arguments go here ) {
//function code goes here
}
And we just call that function like this :
MoveObject.Translation();
However you will notice that he doesn’t declare Translation as static, but instead, uses an intermediate static object called “use” to call it.
The reason he does that is yield.
Inside the Translation function, he uses yield, and static functions cannot yield. Since a static function is the same function regardless of how many scripts define it, it has to be available to be called at any given time. Therefore, if we put yield in a static function (and somehow get Unity to accept this), as long as that function is yielding, it can’t be called again, since its the same function.
What the code in that article does is, it has Translation() as a non-static function, and, in order to be able to call it without GetComponent, he creates the object “use” which is static, and of Type same as the name of the script (MoveObject). Then, in the Awake function, as soon as the script starts executing, he makes sure that the “use” variable contains a copy of MoveObject by doing use = this. The “this” keyword in Java always returns the current class (in our case MoveObject) :
static var use : MoveObject;
function Awake () {
if (use) {
Debug.LogWarning("Only one instance of the MoveObject script in a scene is allowed");
return;
}
use = this;
}
By doing this, he has a static object “use” to call from wherever without the use of a GetComponent, and through the code in that static object (MoveObject code), he creates a non-static instantiation of the Translation function. And since Translation is now non-static, it can yield as well.
It’s just a work-around to avoid using GetComponent and still being able to yield. Instead of creating a static function, he creates a static class that contains a non-static function and accesses that class in order to generate function instances.
As for moveType. It’s an enumeration, and you certainly won’t find it in the documentations, since it’s a custom one. He creates it inside his script by doing this :
enum MoveType {Time, Speed}
No wonder you missed that, it’s a single line at the very beginning of the script.
Enums are variable values. Again, they’re not variables, they’re variable values. In Unity’s source we use enums when we do for example, freezePositionX.
The RigidbodyConstraints is an enumeration with values FreezePositionX, FreezePositionY, FreezePositionZ, FreezeRotationX and so on.
An enumeration is declared as such :
enum Example {
Value1,
Value2,
Value3
}
We declare a variable that uses these values above as :
var someEnumVariable : Example;
And assign values to it as :
someEnumVariable = Example.Value1;
From this point on, the value of someEnumVariable always equals to Value1.
We use enums in situations where a different value type is too cryptic. For instance, assume we are designing an Artificial Intelligence for our NPCs. An NPC always has a state (idle, attacking, fleeing, chasing, etc). We could make a variable called AIState of type integer, and decide that value 0 in that variable is idle, value 1 is attacking, 2 is fleeing, 3 is chasing and so on.
However, it can be tough to remember what each value represents, and since we have no intention of doing any math with these values (combine 2 AIStates for what? It makes no sense), we can just create an enum value of type State like this
enum State {
idle,
attacking,
fleeing,
chasing
}
…and then have the AIState be of type State so we can iterate through states like :
In this case, the value State is both type State AND type int / float depending on the context of the sentence. Therefore we can do both :
var AIState : State = State.idle;
and
var AIState : State = 0;
There’s a bunch more stuff you can do with enums but I don’t want to overwhelm you. I think you get the idea.
Finally,
You should have the answer for this question already based on what I’ve told you above. MoveObject is indeed a class, but the variable used here is “use”, not MoveObject.
Thank you Diviner,
I have gone over this and have some questions.
//////
Static variables.
This is a way in which to retrieve a variable from another script and we can call this variable by simply stating the scripts name, scriptsName.variable = x; Correct? Wow, if so. No special treatment, just scriptName.variableName and presto, you have that info?? Cool. However, this method is only for static variables?
//////
Static Functions
I do not remember but I am puzzled as to why, or how a function would be a variable.
So again, this is a way to call a function, from another script? This is very handy, something I plagued Torque about but with no results. Amazing. This can only happen tho (calling another function altogether or just static?) like this? As in, this is the only way to call a function from another script or just the only way to call a static function from another script?
////
use
The use of “use”. And lack of yield in a static function.
I understand why there can be no yield in a static function. It has the potential to clog and freeze the program. However, what exactly does “use” do? Does it prevent the function from being called if it is in use? (no pun intended). /// And is “use” a declared variable? For instance, could I create a variable called “busy” and use that instead of “use”?
///
enum
I understand the concept based on what you said here, at least giving a state an int value and why you would do that. However, I do not understand how you coded it here. How the enum State{} and subsequent lines of code with in it, make any sense. There are no “;” closers, no numbers, and no instructions for what to do with each. Are these called from other functions. Unsure.
Anyhow, thanks for the tutorial. You truly are Divine.
Obviously you missed my last reply on your previous thread, that’s why you don’t know why and how we declare functions as variables. Check it out, it’s here :
Yes, and it’s only for static members, meaning that these members can’t be instances, they’re the same in every instance. I explained that to you when I was telling you about different variable types. Same applies for static functions.
It’s one of the ways to call a static function from another script and it applies only to static functions. You can always use GetComponent to get that function, but if it’s static, you don’t have to.
For instance, GetComponent itself is a static function of the GameObject class. And you call GetComponent by either doing
GetComponent( ) from within a gameObject, in which case in Unityscript GameObject is implied to exist before GetComponent, or
gameObject.GetComponent() in which case you call the static function by refferencing the current gameObject that carries the script, since all gameObjects are instances of the GameObject class, or
GameObject.GetComponent() in which case you access the class directly.
“use” in our case acts like an intermediary, allowing us to access it by refferencing the class directly (MoveObject) since it’s static, and in turn, it creates instances of the Translation() function. Translation is not static since there couldn’t be more than one threads executing it if it was, but the “use” object generates instances of it (it generates execution threads is a more appropriate term). A static function can only run on a single thread at any given time, but a static object can create multiple execution threads for non-static functions.
That’s just the syntax of enums. You seperate each enum value by a comma and you don’t use semicolons. That’s just the way it is. An enum doesn’t require instructions, it’s a value and not a variable (I already put it twice in bold case in my previous post). You just tell the code what values exist in the State enum. How you use these values is your problem not enum’s. Also you don’t always need numbers in enums, since you’re defining values, and not variables. The numbers I use in my last post are just twin values. The value idle is also a value of 0. However, idle is a value in and by itself, not a variable. We simply can assign two values in each enum, that’s all.
You use an enum in the same way you’d use a number or a word. You don’t call them. They’re values. You assign them and do stuff with them, you check them in if cases, sometimes you add them (if they’re also numbers), you assign them to variables and so on.
Regarding functions (methods) as variables…
does that statement include …
var vel : vector3 = rigidbody.addForce(Vector3.right * 1);
Is it the same philosophy? And again, to reiterate, is it only static functions that can be variables? And perhaps I missed it, but can you provide an example of a function as a variable? I have seen a static function, but not sure I have seen a function as a variable, unless my above example counts.
///
And it is only static functions that can be called from a another script using scriptName.variableName. Correct? If a function tho is not static, it can be called using getComponent() (?). If so, what is the advantage of a static function since Eric has been saying that computation speed is so high now that using getComponent is not that stressful. Is there a distinct advantage to using a static function?
///
I actually have to step out now. Hopefully I can address the rest of this tonight,
if not tomorrow.
Cheers my friend,
Ren
You completely misunderstood more than half of what I told you.
Functions are not variables. They’re declared as variables. The reason you don’t declare them as variables in Unityscript is because all declarations are implicit (public/int/Vector3/etc). Unity implicitly declares them for you when it converts the code from Unityscript to C# once you build your project. If you wrote code in C#, you’d have to do it yourself, since very few things are implied there. Again though, functions are not variables, they’re simply declared as variables.
That’s not to mean functions CAN’T be variables, we have delegation in C# and pseudo-delegation in Unityscript, but make sure you understand the above first before tackling delegates. They’re a bit advanced. So, based on what I just told you:
It would only be true if addForce returned a Vector3 value. But if you check the source :
You will see that AddForce is void :
Notice the very last word in that sentence. And as I said on your other thread, void functions don’t return anything, they’re null.
No function is a variable (except delegates) but all functions are declared as variables, including static ones.
And by that I mean that all functions can be public, private or static and of any Type (int, float, Vector3) as long as they return a value via return;
I’d rather not. You’re stepping into delegate territory here without having properly understood normal functions.
Your above example counts perfectly well for a function that returns a Vector3 value. Just not for addForce. You can use for instance your above example to measure Distance :
var other : Transform;
if (other) {
var dist = Vector3.Distance(other.position, transform.position);
print ("Distance to other: " + dist);
}
Vector3.Distance( ) is a float function, in other words it accepts two Vector3 arguments, calculates the distance between them and returns the result as a float argument.
You are correct. As for the benefit, having cleaner code for one (with GetComponent you need to access the gameObject carrying the script first which if done with Find() or FindWithTag( ) can be both performance heavy and in some cases impossible if for example you don’t know the name of the object that carries the script).
Also, if you don’t use Monobehaviour methods like Start or Update, you don’t need to attach a script in your level to make it work. But you can’t access it without a static intermediary. Take for example an ItemController script for an RPG game.
In that script we only put functions that return information regarding items. For example :
static function getItemName ( itemID : int ) {
var name : String;
switch (itemID) {
case 1:
name = "Healing Potion";
break;
case 2:
name = "Mana Potion";
break;
case 3:
name = "Elixir";
break;
case 4:
name = "Rusty old Sword";
break;
//etc
}
return name;
}
Since I don’t use any Mono functions in this script, I don’t need to drag it on an object in my scene to make it work. And I can do
print( ItemController.getItemName(4) );
from anywhere in my game to get the name of item with ID 4 (Rusty old Sword).
In this case, getItemName( ) is a String function, since it returns a value of type String.
The ItemController example is a good example, because in an RPG, you want access to item data in every level. We would use this function whenever the player accesses his inventory, whenever he picks up loot, and whenever he buys/sells items to a store. Instead of attaching this script in every level on an object and do GetComponent to access it, we can just have it work from the Project tab and access it through script name. Cleaner, faster and less prone to errors.
@renman3000 your over thinking EVERYTHING, forget what you know about torque script and Learn UnityScript, its actually not quite hard to learn when you simplify things.
oh @Diviner your posts are very informative, I love reading them because its good review.