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 ![]()
HAHA, I remember when I asked the same question, here my old code in C#, come with crouch and sprint too
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public bool isSprint = false;
public bool isCrouch = false;
public float moveSpeed = 3;
public float jumpHeight = 2;
public float sprintSpeed = 4;
public float jumpHeightSprint = 2;
public float crouchSpeed = 1.5F;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
//Movement
if(isSprint == false isCrouch == false)
{
float moveX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
float moveZ = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
float moveY = Input.GetAxis("Jump") * jumpHeight * Time.deltaTime;
transform.Translate(moveX,moveY,moveZ);
}
//Sprint Movement
if(isSprint == true)
{
float moveXS = Input.GetAxis("Horizontal") * sprintSpeed * Time.deltaTime;
float moveYS = Input.GetAxis("Jump") * jumpHeightSprint * Time.deltaTime;
float moveZS = Input.GetAxis("Vertical") * sprintSpeed * Time.deltaTime;
transform.Translate(moveXS,moveYS,moveZS);
}
//Sprint Bool
if(Input.GetKeyDown(KeyCode.LeftShift) isSprint == false)
{
isSprint = true;
}
else if(Input.GetKeyUp(KeyCode.LeftShift) isSprint == true)
{
isSprint = false;
}
//Crouch Movement
if(isCrouch == true)
{
float moveXC = Input.GetAxis("Horizontal") * crouchSpeed * Time.deltaTime;
float moveZC = Input.GetAxis("Vertical") * crouchSpeed * Time.deltaTime;
transform.Translate(moveXC,0,moveZC);
}
//Crouch Bool
if(Input.GetKeyDown(KeyCode.LeftControl) isCrouch == false)
{
isCrouch = true;
}
else if(Input.GetKeyUp(KeyCode.LeftControl) isCrouch == true)
{
isCrouch = false;
}
//Animations
//movement ANimations
if(moveSpeed < 1)
{
//idle
animation.CrossFade("idle", 0.2F);
}
if(moveSpeed > 1)
{
//Walk Animations
animation.CrossFade("walk", 0.3F);
}
//Jump
if(jumpHeight > 1)
{
//jumping
animation.CrossFade("jump", 0.3F);
}
//crouch
if(isCrouch == true crouchSpeed > 1)
{
//walk crouch
animation.CrossFade("crouch", 0.2F);
}
if(isCrouch == true crouchSpeed < 1)
{
//idle Crouch
animation.CrossFade("crouch_idle", 0.25F);
}
//Run
if(isSprint == true sprintSpeed > moveSpeed)
{
//Running
animation.CrossFade("running", 0.3F);
}
}
}
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.
You’re in IL also no way
thanks once again.
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.
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.
“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.
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 ![]()