Unity Community


Page 1 of 2 12 LastLast
Results 1 to 20 of 23

  1. Posts
    43

    Simple Player Movement

    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


  2. Location
    Sacramento CA, USA
    Posts
    1,600
    Here is a simple script which will move an object on the x and z axes.

    Code:  
    1. var speed = 5.0;
    2.  
    3. function Update ()
    4. {
    5.      var x = Input.GetAxis ("Horizontal");
    6.      var z = Input.GetAxis ("Vertical");
    7.      transform.Translate (x * Time.deltaTime * speed, 0, z * Time.deltaTime * speed);
    8. }


  3. Posts
    43
    Thank you very much. I got it working =)
    Just one other question why cant i use keycode.w and stuff like this?


  4. Location
    Bergen, Norway
    Posts
    274

  5. Volunteer Moderator
    Posts
    23,719
    Quote Originally Posted by deromega
    why cant i use keycode.w and stuff like this?
    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


  6. Posts
    43
    Quote Originally Posted by Eric5h5
    Quote Originally Posted by deromega
    why cant i use keycode.w and stuff like this?
    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

    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:  
    1. therScript = GetComponent(OtherScript);
    2. 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?

  7. Volunteer Moderator
    Posts
    23,719
    Quote Originally Posted by deromega
    can i acess vars just like this?
    Yep.

    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?
    Yep again. "var test" is a public variable.

    --Eric


  8. Posts
    43
    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


  9. Posts
    43
    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?


  10. Posts
    43
    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?

  11. Volunteer Moderator
    Posts
    23,719
    Quote Originally Posted by deromega
    is it possible to acess positions nd rotations ike this:
    trasform.rotation.z?
    Yep. Except rotations are quaternions and .z isn't what you probably think it is; you'd likely want transform.eulerAngles instead.

    and how can i create an object via script?
    Using prefabs and Instantiate is usually the best way.

    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);
    ?
    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


  12. Posts
    43
    thx
    but i can use eulerangles.x y and z?


  13. Posts
    43
    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


  14. Posts
    43
    maybe you can give me an example of how to create an empty gameobject with transform?
    because tempobject=CreateInstance("tempobject"); doesnt work...


  15. Posts
    1,370
    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:  
    1. this
    refers to this instance, instead of me.

    And yes you can chain dots as long as you like and in any way...


    Code:  
    1. 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;

  16. Volunteer Moderator
    Posts
    23,719
    Quote Originally Posted by deromega
    but i can use eulerangles.x y and z?
    Yep.

    to the third thing you dont understand:
    Still don't get it, sorry. Sounds like maybe you're overcomplicating it; what is the actual end result you want, in general terms?

    maybe you can give me an example of how to create an empty gameobject with transform?
    because tempobject=CreateInstance("tempobject"); doesnt work...
    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


  17. Posts
    43
    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?


  18. Posts
    43
    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?


  19. Posts
    1,370
    var objectA = new GameObject("ObjectA");
    var objectAChild = new GameObject("Child");

    objectAChild.transform.parent = objectA.transform;


  20. Posts
    43
    thank you a lot you were a great help today

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •