Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Simple Player Movement

Discussion in 'Scripting' started by deromega, Jul 28, 2008.

  1. deromega

    deromega

    Joined:
    Jul 28, 2008
    Posts:
    43
    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. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Here is a simple script which will move an object on the x and z axes.

    Code (csharp):
    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. deromega

    deromega

    Joined:
    Jul 28, 2008
    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. MentalFish

    MentalFish

    Joined:
    Nov 2, 2005
    Posts:
    282
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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. deromega

    deromega

    Joined:
    Jul 28, 2008
    Posts:
    43
    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):
    1. otherScript = GetComponent(OtherScript);
    2. otherScript.DoSomething();
    3.  
    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. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep.

    Yep again. "var test" is a public variable.

    --Eric
     
  8. deromega

    deromega

    Joined:
    Jul 28, 2008
    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. deromega

    deromega

    Joined:
    Jul 28, 2008
    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. deromega

    deromega

    Joined:
    Jul 28, 2008
    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. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  12. deromega

    deromega

    Joined:
    Jul 28, 2008
    Posts:
    43
    thx
    but i can use eulerangles.x y and z?
     
  13. deromega

    deromega

    Joined:
    Jul 28, 2008
    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. deromega

    deromega

    Joined:
    Jul 28, 2008
    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. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    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 (csharp):
    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 (csharp):
    1.  
    2. transform.root.Find("player car").GetComponent(RaceCar).wheels[1].renderer.material.SetColor("_Color", FindObjectOfType(ColorGnome).beardColor);
    3.  
    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. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  17. deromega

    deromega

    Joined:
    Jul 28, 2008
    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. deromega

    deromega

    Joined:
    Jul 28, 2008
    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. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    var objectA = new GameObject("ObjectA");
    var objectAChild = new GameObject("Child");

    objectAChild.transform.parent = objectA.transform;
     
  20. deromega

    deromega

    Joined:
    Jul 28, 2008
    Posts:
    43
    thank you a lot you were a great help today :)
     
  21. deromega

    deromega

    Joined:
    Jul 28, 2008
    Posts:
    43
    Code (csharp):
    1.  
    2.     playermodell = GameObject.Find("playera");
    3.     var target =Vector3.zero;
    4.     target.x=playermodell.transform.position.x;
    5.     target.y=0;
    6.     target.z=playermodell.transform.position.z;
    7.     var targetdir=target-Vector3(transform.position.x,0,transform.position.z);
    8.     var forward=transform.forward;
    9.     //Carscript.steerangle_wanted=Vector3.Angle(Vector3(transform.position.x,0,transform.position.z),target);
    10.     Carscript.steerangle_wanted=Vector3.Angle(targetdir,forward);
    11.     targetdir=Quaternion.Euler(Vector3(transform.eulerAngles.x,0,0))*targetdir;
    12.     if(targetdir.z<0)
    13.     {
    14.         Carscript.steerangle_wanted*=-1;
    15.     }
    16.  
    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
     
  22. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    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):
    1.  
    2. // target is a transform we want to drive to.
    3.  
    4. // InverseTransformPoint takes a point in world space and puts it in the transform's local space
    5. var localTarget = transform.InverseTransformPoint(target.position);
    6.  
    7. // localTarget is like how the car (the transform this script is attached to) sees the target
    8. // x+ means it is to the right, and x- means it is to the left. z+ means in front, z- means behind
    9. // the next line is quite complicated. Lets take it apart.
    10. // 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.
    11. // localTarget.x / clamped Z makes the turning fall off smoothly and be based on angle no matter how far away the target is.
    12. // The Clamp around the outside just keeps things in the range of one unit for easy integration with code that accepts user input
    13. var turningInput = Mathf.Clamp(localTarget.x / Mathf.Max(localTarget.z, 0.001), -1, 1);
    14.  
    15. // After this you would do whatever you do to actually make the car turn, depending on the turningInput
    16.  
     
  23. deromega

    deromega

    Joined:
    Jul 28, 2008
    Posts:
    43
    thank you again i know all these lines ^^
    but since id need an exact angle i will replace:
    Code (csharp):
    1.  
    2. var turningInput = Mathf.Clamp(localTarget.x / Mathf.Max(localTarget.z, 0.001), -1, 1);
    by

    Code (csharp):
    1.  
    2. 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):
    1. if(targetdir.z<0)
    2.    {
    3.       Carscript.steerangle_wanted*=-1;
    4.    }
    but your version is more beautyful :p