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 yigitkahramn · Jul 26, 2018 at 06:14 AM · script.scripting beginnerscriptingbasics

Is there a way to recapitulate this?

Hello I'm a newbie around, currently trying to understand how Unity and C# works while experimenting on different game prototypes. I found this piece of code's behaviour to my liking in a WASD controller in Isometric View, but it's almost too complicated for me to understand it. There are if statements everywhere and so on. It takes two objects (1 camera, 1 forward object) to move in a isometric view. I was wondering if there is a way doing that without that forward object (which i didn't understand why it is there) and recapitulate this code with a simple form. Here it is: public class Controller : MonoBehaviour {

     public Transform target;
     public float speed;
     public float runingSpeed;
     float currentSpeed;
     public GameObject GameCamera;
     public Transform objectforward;
     bool isRuning = false;
     Animator anim;
 
     void Start () {
         currentSpeed = speed;
         target.position = transform.position;
         anim = GetComponentInChildren<Animator>();
     }
     
     void Update() {
         Move();
         //target.position = new Vector3(transform.position.x + x, 0, transform.position.z + z) ;
     }
 
     void FixedUpdate(){
         float h = Input.GetAxisRaw("Horizontal");
         float v = Input.GetAxisRaw("Vertical");
         Animating(h, v);
         if (Input.GetKey(KeyCode.LeftShift)){
             bool running = h != 0f || v != 0f;
             anim.SetBool("IsRunning", running);
             anim.SetBool("IsWalking", false);
         }else{
             anim.SetBool("IsRunning", false);
         }
     }
 
     void Move(){
         float relativePosx = target.position.x - transform.position.x;
         float relativePosz = target.position.z - transform.position.z;
 
         float x = Input.GetAxis("Horizontal");
         float z = Input.GetAxis("Vertical");
         if (x >= 1 || x <= -1 || z >= 1 || z <= -1){
 
             Quaternion rotation = Quaternion.LookRotation(new Vector3(relativePosx + 0.0001f, 0, relativePosz + 0.00001f));
             transform.rotation = rotation;
         }
         if (Input.GetKeyDown(KeyCode.LeftShift)){
             isRuning = true;
         }
         if (Input.GetKeyUp(KeyCode.LeftShift)){
             isRuning = false;
         }
         if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D)){
             transform.Translate((Vector3.forward * Time.deltaTime * currentSpeed));
         }
         if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D)){
             currentSpeed = 0;
         }
         if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.S)){
             currentSpeed = 0;
         }
         target.transform.rotation = GameCamera.transform.rotation;
         target.transform.eulerAngles = new Vector3(0, target.transform.eulerAngles.y, 0);
         if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)){
             if (isRuning == false){
                 currentSpeed = speed;
             }else{
                 currentSpeed = runingSpeed;
             }
             target.Translate(Vector3.forward * Time.deltaTime * currentSpeed * 4);
         }
         if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A)){
             if (isRuning == false){
                 currentSpeed = speed;
             }else{
                 currentSpeed = runingSpeed;
             }
             target.Translate(Vector3.right * Time.deltaTime * currentSpeed * 4);
         }
         if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W)){
             if (isRuning == false){
                 currentSpeed = speed;
             }else{
                 currentSpeed = runingSpeed;
             }
             target.Translate(Vector3.back * Time.deltaTime * currentSpeed * 4);
         }
         if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D)){
             if (isRuning == false){
                 currentSpeed = speed;
             }else{
                 currentSpeed = runingSpeed;
             }
             target.Translate(Vector3.left * Time.deltaTime * currentSpeed * 4);
         }
         if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.D)){
             target.transform.position = objectforward.transform.position;
             if (isRuning == false){
                 currentSpeed = speed;
             }else{
                 currentSpeed = runingSpeed;
             }
         }
     }
 
     void Animating(float h, float v){
         bool walking = h != 0f || v != 0f;
         anim.SetBool("IsWalking", walking);
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityCoach · Jul 26, 2018 at 07:50 AM

I've had a quick look at your code. It's hard to say why you do all that, but here's a "clean" version, without all the ifs, misuse of FixedUpdate, mix of GetAxis and GetKey, etc.. It probably no longer does what you want, but it's a clean start, if this is what you want.

 public class Controller : MonoBehaviour
 {
     public Transform target;
 
     public float speed;
     public float runingSpeed;
     float currentSpeed;
 
     public GameObject GameCamera;
 
     bool isRuning = false;
     Animator anim;
 
     Vector3 direction;
 
     private bool _running;
     public bool running
     {
         get { return _running; }
         private set
         {
             if (value != _running)
             {
                 _running = value;
                 anim.SetBool("IsWalking", moving && !_running);
                 anim.SetBool("IsRunning", moving && _running);
 
                 currentSpeed = _running ? runingSpeed : speed;
             }
         }
     }
 
     private bool _moving;
     public bool moving
     {
         get { return _moving; }
         private set
         {
             if (value != _moving)
             {
                 _moving = value;
                 anim.SetBool("IsWalking", _moving && !running);
                 anim.SetBool("IsRunning", _moving && running);
             }
         }
     }
 
     void Start ()
     {
         currentSpeed = speed;
         target.position = transform.position;
         anim = GetComponentInChildren<Animator>();
     }
 
     void Update()
     {
         float relativePosx = target.position.x - transform.position.x;
         float relativePosz = target.position.z - transform.position.z;
 
         direction.x = Input.GetAxis("Horizontal");
         direction.z = Input.GetAxis("Vertical");
 
         moving = direction != Vector3.zero;
         running = Input.GetKey(KeyCode.LeftShift);
 
         transform.Translate((direction * Time.deltaTime * currentSpeed));
 
         target.transform.rotation = GameCamera.transform.rotation;
         target.transform.eulerAngles = new Vector3(0, target.transform.eulerAngles.y, 0);
     }
 }
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

101 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

Related Questions

how can I change a light with multiple triggers. ? 0 Answers

How to lock an int as a playerPref? 1 Answer

How do I improve my world generating script? 0 Answers

is it ok to change the source scripts? 0 Answers

Trying to find the highest number than add it to itself. 2 Answers


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