- Home /
C# Movement Script (Basic)
I just recently changed from Javascript to C#, as many people were telling me that is the better way to go. The problem is, that I need to make a basic movement script, and I need someone kind enough to show me how, give me a link to a tutorial, or even just give me a basic script and a brief discription of what certain parts are.
Thanks alot.
Don't ask for a script, that is one of the most ignorant ways of learning. You wouldn't ask your coworkers to do the work for you, would you? If you need help, say so, if you need scripts this is not the place to ask for them.
I do not think you understand the question, I asked for a script AND explaination...Not just the work.
how do you make the object move faster and jump higher? i tried to modify from speed, jump speed and gravity, but nothing happens.
Daematt, This is probably because you have those variables public which means that if you were to change them in the editor it will override the variable values in the script. So edit the variables from the unity editor not from the script.
@c41v3r13y for future reference. UnityAnswers is designed around helping people who are stuck with solvable issues that have a clear solution (or just one the OP has not found yet)
for a user that is switching from one language to another, or just trying to convert a script from one language to another. the biggest suggestion that can be given is try to take a working script from one language, and then convert it to a different language, and keep compiling it, and fix the errors as they come. if it something about a Unity method (read the docs most of them show examples in all "3" languages). if it is something involving an import/using for cs(go to msdn for unity script it is kind of useful, but not completely).
the biggest differences you will see are
where things are done (unityScript allows for almost any assignment to be done during variable declaration, while in cs many must be done in function space).
the order of things in a declaration.
//js: public var thing : float = 25.0f; //cs: public float thing = 25.0f; //cs(advaced) public var thing = 25.0f;
be careful with this, and there must always be an assignment that follows the var explicit to what it needs to be.
how verbose the code needs to be to accomplish similar things (cs is the more verbose language, and in many cases more specific)
Answer by FLASHDENMARK · Nov 11, 2011 at 06:34 AM
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
//Variables
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.isGrounded) {
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed;
//Jumping
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}
This is a script taken from the Unity script Reference found here.
If you are used to JavaScript, you should be able to figure out the main parts.
Yeah the Unity Script Reference! Why don't more people read that?
It is a quite interesting read you are able to learn something new from time to time.
http://docs.unity3d.com/ScriptReference/CharacterController.$$anonymous$$ove.html is the correct link now.
dude can you make where its only moves the A & D not W & S because i want it to make where they walk left to right not forward not backwards
Answer by zengamer · Apr 07, 2017 at 11:33 PM
// Use this for initialization void Start () {
}
// Update is called once per frame void Update () { if(Input.GetKey(KeyCode.W)){ transform.Translate(Vector3.forward MoveUp Time.deltaTime); } if(Input.GetKey(KeyCode.S)){ transform.Translate(Vector3.back MoveDown Time.deltaTime); } if(Input.GetKey(KeyCode.D)){ transform.Translate(Vector3.right MoveLeft Time.deltaTime); } if(Input.GetKey(KeyCode.A)){ transform.Translate(Vector3.left MoveRight Time.deltaTime); } }
Answer by zedsmith52 · May 18, 2017 at 11:01 AM
For Quake style controls ... Obviously, it's missing a fire button and the instantiate bullet procedure, but that's a whole other kettle of fish!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveMe : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private float turner;
private float looker;
public float sensitivity;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.isGrounded) {
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed;
//Jumping
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
turner = Input.GetAxis ("Mouse X")* sensitivity;
looker = -Input.GetAxis ("Mouse Y")* sensitivity;
if(turner != 0){
//Code for action on mouse moving right
transform.eulerAngles += new Vector3 (0,turner,0);
}
if(looker != 0){
//Code for action on mouse moving right
transform.eulerAngles += new Vector3 (looker,0,0);
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}
@zedsmith52 I copied the scripts you did but it won't work do you know what's going on?