Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Vector 3

Discussion in 'Scripting' started by cruising, Jun 23, 2014.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    I have a code here, but i cant get the character to turn/move on WASD as it should do.
    I guess i need to use vector 3? but i cant figure out how to properly script with it.

    The character just facing forward and do the forward animation no matter what key you press (wasd)

    Heres the code
    Code (CSharp):
    1.         if(Input.GetKey(KeyCode.W)){
    2.         transform.position += transform.forward * movespeed * Time.deltaTime;
    3.            
    4.         }
    5.        
    6.        
    7.         if(Input.GetKey(KeyCode.S)){
    8.         transform.position += transform.forward * +movespeed * Time.deltaTime;  
    9.            
    10.         }
    11.        
    12.        
    13.         if(Input.GetKey(KeyCode.D)){
    14.         transform.position += transform.right * movespeed * Time.deltaTime;  
    15.            
    16.         }
    17.        
    18.        
    19.         if(Input.GetKey(KeyCode.A)){
    20.         transform.position += transform.right * +movespeed * Time.deltaTime;
    21.            
    22.         }
     
  2. vegenarie

    vegenarie

    Joined:
    Jan 5, 2011
    Posts:
    287
  3. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    You should really go through some basic tutorials for Unity3D, especially through the interface which will show you how to use the input settings to make this a lot easier.
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Try doing something like this :

    Code (JavaScript):
    1. var movementSpeed : float = 5;
    2. var rotateSpeed : float = 2.0;
    3.  
    4. function Update(){
    5.  
    6. transform.Rotate(0,Input.GetAxis("Horizontal")* rotateSpeed,0);
    7.  
    8. transform.Translate(0,0,Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime);
    9.  
    10. }
     
  5. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    When you use transform.right and you want to move left it would be smart to subtract value, not to add. :p Same with transform.up and moving down.
    Probably same mistake with animation.

    Code (CSharp):
    1.                if (Input.GetKey (KeyCode.W)) {
    2.                         transform.position += transform.forward * movespeed * Time.deltaTime;
    3.      
    4.                 }  
    5.                 if (Input.GetKey (KeyCode.S)) {
    6.                         transform.position -= transform.forward * +movespeed * Time.deltaTime;
    7.      
    8.                 }
    9.                 if (Input.GetKey (KeyCode.D)) {
    10.                         transform.position += transform.right * movespeed * Time.deltaTime;
    11.      
    12.                 }
    13.                 if (Input.GetKey (KeyCode.A)) {
    14.                         transform.position -= transform.right * +movespeed * Time.deltaTime;
    15.      
    16.                 }
     
    Magiichan likes this.
  6. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    The thing is just to make the character rotate in the same direction as you press the buttons, forward seams to work, but when you press ASD, the character do not rotate right/left/backwards. I have tried the codes here with no success, and
    Intense_Gamer94 code might work if my code was in Java, have some problems with the VARs to work in C.
     
  7. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Javascript is pretty similar to C#

    Code (CSharp):
    1. public float movementSpeed = 5;
    2. public float rotateSpeed = 2.0;
    3.  
    4. void Update(){
    5. transform.Rotate(0,Input.GetAxis("Horizontal")* rotateSpeed,0);
    6. transform.Translate(0,0,Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime);
    7. }
     
  8. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    These floats gives errors ""error CS0664: Literal of type double cannot be implicitly converted to type `float'. Add suffix `f' to create a literal of this type""
     
  9. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    The code that Intense gave works just replace 2.0 with 2. However you should really consider learning a bit more on your own.


    Here is a portion of the code I was talking about before:

    Code (CSharp):
    1.  
    2. // Use FixedUpdate
    3.  
    4. input = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
    5.  
    6. // Add velocity to rigidbody down here
    7.  
    8. // End of required code for movement
    Left the other parts as comments so you can try to find the answer though Intense already gave you the same method pretty much. If you don't understand what FixedUpdate is, Rigidbody, Vector3, GetAxisRaw, ect... you really do need to go through some tutorials, books, or documentation. It's going to save a lot of time.

    Again your four if statements can be easily made into 2 lines for movement if you simply go follow some tutorials that I have no pasted below.



    Maybe watch one on making a 2D game to get some practical experience:



    and of course search documentation for how these things work or the web in general and do what we all do, trial and error. Really, it helps more than you to use these tutorials.
     
    Last edited: Jun 24, 2014