Hi i am new here and i want to make my very first steps in unity. I already finished half of the Platformer tutorial but it seems like i am not able to get a working player movement. it should be as simple as possible. i am a very good scripter so the physics stuff doesnt matter i only need a clue about how to create a model and move it in the x and z axis with the keys wasd. i dont want to use templates or something like this and please in javascript or c# (btw i used the search function but wasnt able to find anything usefull) thank you omega
Here is a simple script which will move an object on the x and z axes. Code (csharp): var speed = 5.0; function Update () { var x = Input.GetAxis ("Horizontal"); var z = Input.GetAxis ("Vertical"); transform.Translate (x * Time.deltaTime * speed, 0, z * Time.deltaTime * speed); }
Thank you very much. I got it working =) Just one other question why cant i use keycode.w and stuff like this?
Why do you assume you can't? http://unity3d.com/support/documentation/ScriptReference/Input.GetKey.html
It's better not to. Using GetAxis makes your code simpler, you automatically support keyboards/joysticks/joypads with the same code, and players can change the input settings to their liking without you having to code anything extra (at least with standalones, but not with webplayers). --Eric
ah now i see i tried just to use keycode.w-keycode.s which was wrong :wink: well another question: i have some agents in my project and i want them to set some arrays and vars in an other scriptfile. how do i do that? Code (csharp): otherScript = GetComponent(OtherScript); otherScript.DoSomething(); i found this in the reference. can i acess vars just like this? otherScript.array[anylocalnumber]=... and which kind of vars can i acess do they have to be public or is it okay when they are jus like var test; And i hope i am right when i think that i cant acess private vars?
okay thank you again! is it possible to acess positions nd rotations ike this: trasform.rotation.z? and how can i create an object via script? and is it possible to store handles like this? otherscript.pointerarray[otherscript.counter]=transform; so the otherscript can acess objects and move them for example like this: movevector=vec3(1,2,3); pointerarray[wanttomovethis].translate(movevector*time.deltatime); ? thx again
Some other questions: I want to create a player controlled physics car so i have the mesh and the wheels i created a gameobejct and added a box collider to the game obejct with the right size and added a wheel colider to the wheels. So what to do now? Add a rigidbody to the gameobject? if i do that the car fals down which is a good thing but through the street model i have added so how can i fix it?
I know now why he was falling through the ground. because the ground had no collider. But the wheels dont hit the ground and i dont know why? what should i do?
Yep. Except rotations are quaternions and .z isn't what you probably think it is; you'd likely want transform.eulerAngles instead. Using prefabs and Instantiate is usually the best way. Not sure I quite understand...do you want to move an object from a different script? If so, you'd use the object's Transform directly. About the car stuff, there are quite a few topics about car physics if you do a search. --Eric
to the third thing you dont understand: i have the otherscript and what the otherscript to be able to acess the object that acessed the otherscript so the otherscript has a var fr example firstscript so the otherscript can do this: firstscript.transform.translate(1,1,1); after the first script has setted otherscript.firstscript=me; and i dont know how to replace the me by code which works
maybe you can give me an example of how to create an empty gameobject with transform? because tempobject=CreateInstance("tempobject"); doesnt work...
You can find a lot of these unity specific things in the documentation, for example if you look up the page for GameObject you will find the GameObject.CreatePrimitive function and the GameObject constructor that makes new empty objects. Code (csharp): this refers to this instance, instead of me. And yes you can chain dots as long as you like and in any way... Code (csharp): transform.root.Find("player car").GetComponent(RaceCar).wheels[1].renderer.material.SetColor("_Color", FindObjectOfType(ColorGnome).beardColor); However, of course, that code is _Horrible_ as far as speed and risk of null references. It is much better to just keep pointers to everything you need in variables and either assign them in the inspector or assign them in the Start function. If you want to access the rotation values that you see in the inspector, that is transform.localEulerAngles which can be read and set just fine, including component wise in javascript. theTransform.localEulerAngles.x += 10;
Yep. Still don't get it, sorry. Sounds like maybe you're overcomplicating it; what is the actual end result you want, in general terms? CreateInstance is for ScriptableObjects. What you'd normally use is Instantiate. However if all you really want is just an empty game object, do "tempobject = new GameObject();" --Eric
well the generel result is: for example i have a controller object which should move some obejects around and i dont know the number of objects and wnat to be able to create some objects and still want them to be controlled by the controller. So the controller has an array and one array var represents one of the objects to move around but first i have to set the array up so i have a function of the moveobject which acesses the controller which is an easy thing and sets one of the array var to "this" so the controller knows the objects to move and can do it like this array[1].transform.translate(1,1,1); this is just an example but this method is very important for the stuff i am scripting (i am mostly in artificial intelligence) i hope you understand now what i mean?
tempobject=new GameObject(); tempobjectb=tempobject.new GameObject(); how can make this working? so i can use tempobject.tempobjectb so if i move or rotate tempobject tempobjectb will move or rotate too?
var objectA = new GameObject("ObjectA"); var objectAChild = new GameObject("Child"); objectAChild.transform.parent = objectA.transform;
Code (csharp): playermodell = GameObject.Find("playera"); var target =Vector3.zero; target.x=playermodell.transform.position.x; target.y=0; target.z=playermodell.transform.position.z; var targetdir=target-Vector3(transform.position.x,0,transform.position.z); var forward=transform.forward; //Carscript.steerangle_wanted=Vector3.Angle(Vector3(transform.position.x,0,transform.position.z),target); Carscript.steerangle_wanted=Vector3.Angle(targetdir,forward); targetdir=Quaternion.Euler(Vector3(transform.eulerAngles.x,0,0))*targetdir; if(targetdir.z<0) { Carscript.steerangle_wanted*=-1; } why doesnt this work probaly? the car should drive towards the player but it behaves weird... EDIT: better explained: the steerangle+transformangle should point towards the player
Vector3.Angle returns just an angle, it is nothing spatial, IE it is a positive float from 0 to 180. You are trying to use it in a spatial way (guide a car with it). You need the dynamic range of positive and negative to drive a car. Vector3.Angle is the wrong function. Code (csharp): // target is a transform we want to drive to. // InverseTransformPoint takes a point in world space and puts it in the transform's local space var localTarget = transform.InverseTransformPoint(target.position); // localTarget is like how the car (the transform this script is attached to) sees the target // x+ means it is to the right, and x- means it is to the left. z+ means in front, z- means behind // the next line is quite complicated. Lets take it apart. // Mathf.Max(localTarget.z, 0.001) avoids dividing by 0 or negative numbers, and makes the car turn sharply when the target is behind it. // localTarget.x / clamped Z makes the turning fall off smoothly and be based on angle no matter how far away the target is. // The Clamp around the outside just keeps things in the range of one unit for easy integration with code that accepts user input var turningInput = Mathf.Clamp(localTarget.x / Mathf.Max(localTarget.z, 0.001), -1, 1); // After this you would do whatever you do to actually make the car turn, depending on the turningInput
thank you again i know all these lines ^^ but since id need an exact angle i will replace: Code (csharp): var turningInput = Mathf.Clamp(localTarget.x / Mathf.Max(localTarget.z, 0.001), -1, 1); by Code (csharp): var turningInput = Mathf.Atan2(localTarget.x / Mathf.Max(localTarget.z, 0.001))* Mathf.Rad2Deg; i already implemented an other version of how to make the angle postive or negative: Code (csharp): if(targetdir.z<0) { Carscript.steerangle_wanted*=-1; } but your version is more beautyful