Search Unity

Scripting Tutorial Moving Player Help.

Discussion in 'Scripting' started by happytodd, Dec 1, 2009.

  1. happytodd

    happytodd

    Joined:
    Dec 1, 2009
    Posts:
    7
    Dear users/staff,
    I have been messing around with Unity for about a week now and I just started doing the scripting tutorials today. However I have a few questions. I did the basic movement with a cube using WASD keys which works fine, but then I tried to apply the script to a downloaded .max model of a car I downloaded from turbosquid.com. When I run the game and press up and down arrows the model will go up and down on the Y axis. Instead of moving forwards or backwards. Is anyone able to explain to me why this is happening? Also if I put a rigidbody on the model it will tend to fall through the whole platform unless it has a mesh collider, but if I wanted the car to roll, I'd need wheel colliders right? Sorry for being a noob. Just need help understanding these basics first.

    From Todd Drexel (happytodd)
     
  2. nasawhy

    nasawhy

    Joined:
    Nov 28, 2009
    Posts:
    18
    It sounds like your car model might not be oriented correctly in 3ds? Is there a chance that Y points toward the front of your car in 3ds?

    But then again you would think that if your model was made by a "pro" this wouldn't be the case.

    What happens if you adjust the rotation after import within the Inspector panel?

    just thinking out loud... good luck on the other stuff!
     
  3. happytodd

    happytodd

    Joined:
    Dec 1, 2009
    Posts:
    7
    Ahh, Rotation is set at 270x. If I change it to 0 the front of the car will be facing in the ground. Is there away I could configure the script
    Code (csharp):
    1. var speed = 5.0;
    2. function Update () {
    3. var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    4. var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    5. transform.Translate(x, 0, z);
    6. }
    To allow the car be at 270x rotation but move fowards on another axis. Damn this is hurting my head thinking about it now. Thanks for helping me understand the rotation part =]
     
  4. nasawhy

    nasawhy

    Joined:
    Nov 28, 2009
    Posts:
    18
    It would probably be better if you could adjust the model so that when you import it, it's oriented to the axes correctly. If you were to edit the current script to compensate and then start doing other things and customize some other scripts, it's going to get really confusing and probably more complicated if you always have to account for this... your brain will hurt perpetually. :)

    Otherwise, I suppose you could try subbing all occurrences of "z" with "y" in the script and see how messed up (or not) it turns out.
     
  5. happytodd

    happytodd

    Joined:
    Dec 1, 2009
    Posts:
    7
    Yeah the script seems to be messing up. I just downloaded 3ds Max Studio 2010. I have no idea how to use it. So I should learn abit more about it and try figure out how to rotate the whole model 270degrees. Thanks for the help though I appreciate it =]
     
  6. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Yep i use that script but my object don´t move forward?? Onli left, right, up and down???

    How to move my object forward??
     
  7. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If the up and down arrow keys are moving your object up and down in the scene, you are probably using the Y axis for forward/backward in your game. You can put this right by changing the axis in the transform.Translate call:-
    Code (csharp):
    1. transform.Translate(x, z, 0);
     
  8. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    it works But !!??

    when i press "S" it go forward, and "W" Go back???

    How to rotate the object when i turn left or right?

    here is my project files for help me out.

    http://www.megaupload.com/?d=ZLP9VWIF
     
  9. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You just need to negate the result of Input.GetAxis to fix that:-
    Code (csharp):
    1. var z = -Input.GetAxis("Vertical") * Time.deltaTime * speed;
    There is a transform.Rotate function to do this for you.
     
  10. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    I made this way it works but?:)

    var speed = 5.0;
    var rotationSpeed = 100.0;

    function Update ()
    {
    var x = Input.GetAxis("Horizontal") * Time.deltaTime * rotationSpeed;
    var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    transform.Translate(x, z, 0);
    transform.Rotate(Time.deltaTime, 0, x);
    }

    But is a little strange turn? i tink need now an mesh rotation, to give elusion of turning!?

    I dont found any reference for this action.

    any tip?