Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by UnityMasterkk · Dec 11, 2019 at 03:10 AM · controllerscripting beginner

Is my controller script good?

This is my first code in C#, Is it good? I used RigidBody to get better collisions. using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class movimento : MonoBehaviour
 {
     public Rigidbody rigid;
     public float VelocidadeAtual, VelocidadeSprint;
 
     // Start is called before the first frame update
     void Start()
     {
         rigid = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey("w"))
         {
             rigid.AddForce(transform.forward * VelocidadeAtual, ForceMode.Acceleration);
         }
         if (Input.GetKey("s"))
         {
             rigid.AddForce(transform.forward * -VelocidadeAtual, ForceMode.Acceleration);
         }
         if (Input.GetKey("a"))
         {
             rigid.AddForce(transform.right * -VelocidadeAtual, ForceMode.Acceleration);
         }
         if (Input.GetKey("d"))
         {
             rigid.AddForce(transform.right * VelocidadeAtual, ForceMode.Acceleration);
         }
         if (Input.GetKey(KeyCode.LeftShift))
         {
             VelocidadeAtual = VelocidadeSprint;
         }
         if (Input.GetKeyUp(KeyCode.LeftShift))
         {
             VelocidadeAtual = 10;
         }
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Xamtox · Dec 11, 2019 at 07:24 AM

I can think of several improvements:

  1. Class name movimento should start with capital letter.

  2. Component initialization is better to move to Awake() instead of Start(). It's irrelevant in your case, but better practice in general.

  3. Instead of public is better to use [SerializeField] private. Then you still can configure your values via inspector, but cannot mess with your variables outside the script.

  4. You could combine horizontal ('a' and 'd') and vertical ('w' and 's') movements, and sprint logic can be written in single line.

Example:

 private Rigidbody rigid;
 [SerializeField] private VelocidadeSprint = 20;
 [SerializeField] private VelocidadeAtual = 10;

 void Awake()
 {
     rigid = GetComponent<Rigidbody>();
 }

 void Update()
 {
     float speed = Input.GetKey(KeyCode.LeftShift) ? VelocidadeSprint : VelocidadeAtual;
    
     Vector3 forward = Input.GetAxis("Vertical") * transform.forward;
     Vector3 sideways = Input.GetAxis("Horizontal") * transform.right;
     rigid.AddForce((forward + sideways) * speed, ForceMode.Acceleration);
  }

In that case speed will always be 20 when shift key is pressed and 10 otherwise.

Also, you could use [RequireComponent(typeof(Rigidbody))] attribute on your movimento class to ensure there always will be a Rigidbody.

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
0

Answer by UnityMasterkk · Dec 12, 2019 at 02:52 PM

Thank you @Deygon.

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

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

129 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Controlling orbiting camera with mouse AND controller? 0 Answers

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to make a dodge command/travel a certain distance in a forward direction based on current forward vector then stop? 0 Answers

3rd person rigidbody controller help :( 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