Unity Community


Results 1 to 8 of 8

  1. Location
    Chicago, IL
    Posts
    213

    Player move script help

    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


  2. Location
    Illinois
    Posts
    254
    HAHA, I remember when I asked the same question, here my old code in C#, come with crouch and sprint too
    Code:  
    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. }
    Leader Of Team Nocks


    Have You Seen A Unity Video/Game/Picture That's Looks Amazing, Show The Rest Of Us:


    http://forum.unity3d.com/threads/194...al-Showcase!!!


  3. Location
    Chicago, IL
    Posts
    213
    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. Location
    Chicago, IL
    Posts
    213
    You're in IL also no way thanks once again.


  5. Location
    Illinois
    Posts
    254
    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.
    Leader Of Team Nocks


    Have You Seen A Unity Video/Game/Picture That's Looks Amazing, Show The Rest Of Us:


    http://forum.unity3d.com/threads/194...al-Showcase!!!


  6. Location
    Chicago, IL
    Posts
    213
    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. Posts
    289
    "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. Location
    Chicago, IL
    Posts
    213
    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

Posting Permissions

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