• 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 /
This question was closed Oct 11, 2015 at 09:27 PM by ShadowMolasses for the following reason:

Too subjective and argumentative

avatar image
Question by ShadowMolasses · Oct 08, 2015 at 08:51 PM · float

Having trouble with Isometric movement

Hey everyone, for starters id like to just say this is my first time scripting anything ever so I apologize for my ignorance. im trying to create a beat em up style game similar to castle crushers or scott pilgrim vs the world. things have been going smoothly and ive been able to get by on stitching so code together found in various different tutorial vidoes to get something that works but ive hit a snag, currently my character only moves on a Horizontal axis, and i want to apply a Vertical axis so he can move around like the games i mentioned before.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControllerScript : MonoBehaviour
 {
     public float maxSpeed = 6f;
     bool faceingRight = true;
 
     Animator anim;
 
     void Start()
     {
         anim = GetComponent<Animator> ();
     }
 
 
     void FixedUpdate()
     {
         float move = Input.GetAxisRaw ("Horizontal");
         float move = Input.GetAxisRaw ("Vertical");
 
         anim.SetFloat ("Speed", Mathf.Abs(move));
 
         GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
 
         if (move > 0 && !faceingRight)
             Flip ();
         else if (move < 0 && faceingRight)
             Flip ();
     }
 
     void Flip()
     {
         faceingRight = !faceingRight;
         Vector3 TheScale = transform.localScale;
         TheScale.x *= -1;
         transform.localScale = TheScale;
     }
 }
 

Everything worked untill i applied the " float move = Input.GetAxisRaw ("Vertical");" I get an error saying "error CS0128: A local variable named 'move' is alreadt defined in this scope"

Again this is my first time scriptigng so i apologize for my frankinstiend scripting, i mostly pulled from what i could find and didnt make this myself, scripting and logic was never something i could wrap my brain around so im having trouble understanding it even with all the tutorials.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by dkjunior · Oct 08, 2015 at 10:13 PM

You have two variables with the same name "move" in the same scope, and hence you get this compilation error. Give them different names:

 float moveH = Input.GetAxisRaw ("Horizontal");
 float moveV = Input.GetAxisRaw ("Vertical");
 
 GetComponent<Rigidbody2D>().velocity = maxSpeed * new Vector2(moveH , moveV);

Comment

People who like this

0 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 ShadowMolasses · Oct 08, 2015 at 10:28 PM 0
Share

Tried that, and modified all my other "move" variables to "moveH and moveV" however im still getting errors

 if (moveH , moveV > 0 && !faceingRight)
             Flip ();
    

here^

 else if (moveH , moveV < 0 && faceingRight)
             Flip ();

and here^

avatar image dkjunior · Oct 08, 2015 at 10:37 PM 0
Share

This syntax is incorrect. What exactly are you trying to achieve in these if statements?

avatar image ShadowMolasses dkjunior · Oct 09, 2015 at 02:11 AM 0
Share

if i change the direction the sprite will flip accordingly

avatar image dkjunior · Oct 09, 2015 at 04:52 PM 0
Share

You can keep your code for flipping in horizontal direction (just change move to moveH) in the if statements. You will also need to handle flips in vertical direction, something like:

 if (moveV > 0 && !facingUp)
     FlipVert();
 else if (moveV < 0 && facingUp)
     FlipVert();

 void FlipVert()
 {
     facingUp = !facingUp;
     Vector3 TheScale = transform.localScale;
     TheScale.y *= -1;
     transform.localScale = TheScale;
 }

Now, as you can see, there's plenty of code duplication if you just put it like this. I'll leave it up to you as an exercise to combine the code for horizontal/vertical flipping. Good luck!

avatar image ShadowMolasses dkjunior · Oct 10, 2015 at 04:04 AM 0
Share

I dot totally understand how code works yet, i just cant understand it no matter how much i watch, i thank your for helping me out and showing me some of your know how. now i just gotta figurew this code out

avatar image ShadowMolasses ShadowMolasses · Oct 10, 2015 at 05:28 AM 0
Share
 public class PlayerControllerScript : MonoBehaviour
 {
     public float maxSpeed = 6f;
     bool faceingRight = true;
 
     Animator anim;
 
     void Start()
     {
         anim = GetComponent<Animator> ();
     }
 
 
     void FixedUpdate()
     {
         float moveH = Input.GetAxisRaw ("Horizontal");
         float moveV = Input.GetAxisRaw ("Vertical");
 
         GetComponent<Rigidbody2D> ().velocity = maxSpeed * new Vector2 (moveH , moveV);
 
         anim.SetFloat ("Speed", Mathf.Abs(moveH , moveV));
 
         GetComponent<Rigidbody2D>().velocity = new Vector2(moveH , moveV * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
 
         if (moveV > 0 && !facingUp)
             FlipVert();
         else if (moveV < 0 && facingUp)
             FlipVert();
         
         void FlipVert()
         {
             facingUp = !facingUp;
             Vector3 TheScale = transform.localScale;
             TheScale.y *= -1;
             transform.localScale = TheScale;
         }
 
         if (moveH > 0 && !facingUp)
             FlipHorz();
         else if (moveV < 0 && facingUp)
             FlipHorz();
         
         void FlipHorz()
         {
             facingUp = !facingUp;
             Vector3 TheScale = transform.localScale;
             TheScale.y *= -1;
             transform.localScale = TheScale;
         }
     }

Will this work?

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do i get the part of a float after the point? 7 Answers

check if an always increasing variable has stop increasing 2 Answers

Convert Text to float 3 Answers

Float to Byte for Color32 3 Answers

How does Input.GetAxis work with a keyboard? 3 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