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

Player move script help

Discussion in 'Scripting' started by iWoundPwn, Mar 24, 2013.

  1. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    Help I need help with how to create a script that I can assign to an object that has the if (Input.GetKey("w")){ then the player will move forward etc i'm new to code and don't like to use the default one, thank you guys once again for the help :D
     
  2. tylernocks

    tylernocks

    Joined:
    Sep 9, 2012
    Posts:
    256
    HAHA, I remember when I asked the same question, here my old code in C#, come with crouch and sprint too
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour {
    5.    
    6.     public bool isSprint = false;
    7.     public bool isCrouch = false;
    8.     public float moveSpeed = 3;
    9.     public float jumpHeight = 2;
    10.     public float sprintSpeed = 4;  
    11.     public float jumpHeightSprint = 2;
    12.     public float crouchSpeed = 1.5F;  
    13.     // Use this for initialization
    14.     void Start () {
    15.    
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update ()
    20.     {
    21.   //Movement
    22.     if(isSprint == false  isCrouch == false)
    23.     {      
    24.   float moveX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
    25.   float moveZ = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
    26.   float moveY = Input.GetAxis("Jump") * jumpHeight * Time.deltaTime;
    27.   transform.Translate(moveX,moveY,moveZ);
    28.         }
    29.    
    30.        
    31.     //Sprint Movement
    32.         if(isSprint == true)
    33.         {      
    34.   float moveXS = Input.GetAxis("Horizontal") * sprintSpeed * Time.deltaTime;
    35.   float moveYS = Input.GetAxis("Jump") * jumpHeightSprint * Time.deltaTime;
    36.   float moveZS = Input.GetAxis("Vertical") * sprintSpeed * Time.deltaTime;
    37.   transform.Translate(moveXS,moveYS,moveZS);
    38.         }
    39.     //Sprint Bool  
    40.         if(Input.GetKeyDown(KeyCode.LeftShift)  isSprint == false)
    41.         {
    42.             isSprint = true;
    43.         }
    44.             else if(Input.GetKeyUp(KeyCode.LeftShift)  isSprint == true)
    45.         {
    46.             isSprint = false;
    47.         }
    48.            
    49.        
    50.         //Crouch Movement
    51.         if(isCrouch == true)
    52.         {      
    53.   float moveXC = Input.GetAxis("Horizontal") * crouchSpeed * Time.deltaTime;
    54.   float moveZC = Input.GetAxis("Vertical") * crouchSpeed * Time.deltaTime;
    55.   transform.Translate(moveXC,0,moveZC);
    56.         }
    57.        
    58.     //Crouch Bool  
    59.         if(Input.GetKeyDown(KeyCode.LeftControl)  isCrouch == false)
    60.         {
    61.             isCrouch = true;
    62.         }
    63.             else if(Input.GetKeyUp(KeyCode.LeftControl)  isCrouch == true)
    64.         {
    65.             isCrouch = false;
    66.         }
    67.        
    68.         //Animations
    69.         //movement ANimations
    70.         if(moveSpeed < 1)
    71.         {
    72.             //idle
    73.             animation.CrossFade("idle", 0.2F);
    74.         }
    75.        
    76.         if(moveSpeed > 1)
    77.         {
    78.             //Walk Animations
    79.             animation.CrossFade("walk", 0.3F);
    80.         }
    81.        
    82.         //Jump
    83.         if(jumpHeight > 1)
    84.         {
    85.             //jumping
    86.             animation.CrossFade("jump", 0.3F);
    87.         }
    88.        
    89.         //crouch
    90.         if(isCrouch == true  crouchSpeed > 1)
    91.         {
    92.             //walk crouch
    93.             animation.CrossFade("crouch", 0.2F);
    94.         }
    95.         if(isCrouch == true  crouchSpeed < 1)
    96.         {
    97.             //idle Crouch
    98.             animation.CrossFade("crouch_idle", 0.25F);
    99.         }
    100.        
    101.         //Run
    102.         if(isSprint == true  sprintSpeed > moveSpeed)
    103.         {
    104.             //Running
    105.             animation.CrossFade("running", 0.3F);
    106.         }  
    107. }
    108. }
     
  3. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    Thanks and what do you recommend is better C# or JavaScript? I know a lot of people go with C# but i'm in the middle of javascript don't know if I should switch over.
     
  4. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    You're in IL also no way :D thanks once again.
     
  5. tylernocks

    tylernocks

    Joined:
    Sep 9, 2012
    Posts:
    256
    Well......I wont tell you what to do, but it seems more logical to do JavaScript because its easier and you have more tutorials...but I don't like JavaScript and I had to work much harder to learn C#, because I just find java script to be....weird.
     
  6. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    I'm just asking which one is better in the long run C# to me looks easier, but either way if JavaScript is easier i'll learn that first, yes javascript is weird to me also.
     
  7. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    "Better" is very relative:
    There is very little equivalent differences between Unity's "Javascript" implementation and a C# implementation. They have comparable performance at run time. There are a few times when a solution in either language appears easier / better / quicker to code (C# delegates come to mind) and if you're coding in one of those areas then the choice will be a lot clearer.

    All things being equal your own personal coding experience will likely tell you which is easier i.e. which one based on your past experience is easier to learn. I've used a lot of java like languages in the past so in my case "Better" was Unity's Javascript.

    In the real world though were all things are not equal C# will be the choice to go with. Why? Because Unity's javascript is only valid inside Unity3d. It's not a fully implemented cross-platform type of implementation and will likely require a lot of rewrite to be of use outside of the Unity3d environment where as C# is more likely to be directly portable. There's also a lot more support for C# based editors which have a ton of features to help make your code life a bit easier.
     
  8. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    Thanks BPPHarv for the detailed explanation then my choice of language for Unity will be JavaScript. I'm going to start learning C# then after JavaScript just in case I decide to make a game on another engine, but for now I don't see me moving anywhere else. Unity is very amazing and so is the community! Thanks man :D