Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by C41V3R13Y · Nov 11, 2011 at 05:54 AM · c#

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FLASHDENMARK · Nov 11, 2011 at 06:38 AM 1
Share

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.

avatar image C41V3R13Y · Nov 15, 2011 at 05:04 AM 1
Share

I do not think you understand the question, I asked for a script AND explaination...Not just the work.

avatar image Daematt · Apr 24, 2013 at 10:33 AM -1
Share

how do you make the object move faster and jump higher? i tried to modify from speed, jump speed and gravity, but nothing happens.

avatar image Mayday556 · Aug 05, 2013 at 05:02 PM -1
Share

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.

avatar image gardian06 · Aug 05, 2013 at 05:19 PM -1
Share

@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)

3 Replies

· Add your reply
  • Sort: 
avatar image
28

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Nov 11, 2011 at 06:40 AM 0
Share

Yeah the Unity Script Reference! Why don't more people read that?

avatar image FLASHDENMARK · Nov 11, 2011 at 06:43 AM 0
Share

It is a quite interesting read you are able to learn something new from time to time.

avatar image twobob · Apr 15, 2015 at 03:43 PM 0
Share

http://docs.unity3d.com/ScriptReference/CharacterController.$$anonymous$$ove.html is the correct link now.

avatar image segahogzombie · Aug 04, 2015 at 04:13 PM 0
Share

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

avatar image megostodios · Dec 23, 2016 at 10:27 AM 1
Share

it did not work

Show more comments
avatar image
1

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); } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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);
     }
 }



Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image downwriver · Jan 01, 2021 at 08:41 AM 0
Share

@zedsmith52 I copied the scripts you did but it won't work do you know what's going on?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges