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
1
Question by Jayby18 · Jul 05, 2017 at 04:35 PM · movement2d gameplayer2d-platformertransform.position

My Player doesn't move anymore. I didn't change a thing in the script and it did work before. What's wrong?

I'm making a 2D game and so far everything worked. But all of a sudden, today, my Player wouldn't move anymore. I didn't change anything that the player had. Here is 'Player.cs': using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;

 [RequireComponent(typeof(PlayerMovement))]
 [RequireComponent(typeof(Animator))]
 public class Player : MonoBehaviour
 {
     [SerializeField]
     private PlayerMovement pm;
     private Animator anim;
     private SpriteRenderer rend;
     private PlayerCombat pc;
     
     public int health = 5;
     public int exp = 0;
     
     public int currentEnemyCount;
 
     public float speed;
     public float walkSpeed = 5f;
     public float runSpeed = 10f;
     public float jumpSpeed = 7f;
     
     public bool isGrounded;
 
     void Start()
     {
         pm = GetComponent<PlayerMovement>();
         anim = GetComponent<Animator>();
         rend = GetComponentInChildren<SpriteRenderer>();
         pc = GetComponentInChildren<PlayerCombat>();
         
         Physics.IgnoreLayerCollision(10, 10, true);
     }
 
     void Update()
     {
         //Movement speed:
         if(Input.GetKey(KeyCode.LeftShift))
         {
             speed = runSpeed;
         }
         else
         {
             speed = walkSpeed;
         }
 
         if(GameObject.Find("DialogManager").GetComponent<DialogManager>().GetActiveSelf() == false)
         {
             Movement();
             
             //Jumping:
             if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
             {
                 Debug.Log("Jumping...");
                 pm.Jump(jumpSpeed);
             }
         }
         
         //Escape to main menu:
         if(Input.GetKeyDown(KeyCode.Escape))
         {
             //Make sure main menu is always scene number 0!!!
             SceneManager.LoadScene(0);
         }
     }
 
     void Movement()
     {
         float xMov = Input.GetAxis("Horizontal");
         float zMov = Input.GetAxis("Vertical");
         
         if(xMov != 0 || zMov != 0)
         {
             anim.SetBool("Moving", true);
         }
         else
         {
             anim.SetBool("Moving", false);
         }
         
         if(xMov < 0)
         {
             rend.flipX = true;
         }
         else if(xMov > 0)
         {
             rend.flipX = false;
             Debug.Log("Moving right");
         }
 
         Vector2 movHorizontal = transform.right * xMov;
         Vector2 movVertical = transform.forward * zMov;
 
         Vector2 velocity = (movHorizontal + movVertical) * speed;
 
         pm.MoveHorizontal(velocity);
     }
     
     public void ApplyDamage(int damage)
     {
         health -= damage;
         if(health <= 0)
         {
             KillYourself();
         }
         else if(health == 1)
         {
             rend.color = Color.red;
         }
         else
         {
             Respawn();
         }
     }
     
     public void GainExperiencePoints(int points)
     {
         exp += points;
     }
     
     public void CountEnemy()
     {
         currentEnemyCount--;
         
         if(currentEnemyCount <= 0)
         {
             CompleteLevel(-1);
         }
     }
     
     public void CompleteLevel(int nextLevel)
     {
         Debug.Log("Level Complete!");
         
         Scene currentScene = SceneManager.GetActiveScene();
         
         if(nextLevel == -1)
         {
             nextLevel = currentScene.buildIndex + 1;
         }
         
         SceneManager.LoadScene(nextLevel);
     }
     
     void KillYourself()
     {
         Destroy(gameObject);
         GameObject HUD = GameObject.Find("HUDManager");
         HUD.GetComponent<HUDManager>().ActivateDeathScreen();
     }
     
     void Respawn()
     {
         transform.position = new Vector2(0, 1);
     }
     
     void OnCollisionEnter2D(Collision2D collision)
     {
         GameObject obj = collision.gameObject;
         if(obj.tag == "KillZone")
         {
             ApplyDamage(1);
         }
         else if(obj.tag == "ThrowablePickup")
         {
             pc.currentAmmo++;
         }
     }
     
     void OnCollisionStay2D(Collision2D collision)
     {
         GameObject obj = collision.gameObject;
         if(obj.layer == 8)
         {
             isGrounded = true;
         }
     }
     
     void OnCollisionExit2D(Collision2D collision)
     {
         GameObject obj = collision.gameObject;
         if(obj.layer == 8)
         {
             isGrounded = false;
         }
     }
 }

And here is 'PlayerMovement.cs':

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody2D))]
 public class PlayerMovement : MonoBehaviour
 {
     [SerializeField]
     private Player player;
     private Rigidbody2D rb;
     
     void Start()
     {
         player = GetComponent<Player>();
         rb = GetComponent<Rigidbody2D>();
         rb.constraints = RigidbodyConstraints2D.FreezeRotation;
     }
     
     public void MoveHorizontal(Vector2 _velocity)
     {
         rb.MovePosition(rb.position + _velocity * Time.fixedDeltaTime);
     }
     
     public void Jump(float _thrust)
     {
         rb.AddForce(new Vector2(0, _thrust));
     }
 }

This code worked for me before, but it suddenly didn't. And I also have a sword that glitches: it flashes in two spots and moves weird. I put a script on it to follow the player:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(PlayerCombat))]
 public class PlayerWeapon : MonoBehaviour 
 {
     public float weaponDamage = 10f;
     
     public float offSetX;
     public float offSetY;
     
     private GameObject player;
     
     void Start()
     {
         player = GameObject.FindWithTag("Player");
         Physics2D.IgnoreCollision(player.GetComponent<Collider2D>(), GetComponent<Collider2D>());
     }
     
     void Update()
     {
         float xaxis = player.transform.position.x;
         float yaxis = player.transform.position.y;
         transform.position = new Vector2(xaxis + offSetX, yaxis + offSetY);
         //0.35f, 0.03f
     }
     
     void OnCollisionEnter2D(Collision2D collision)
     {
         GameObject obj = collision.gameObject;
         
         if(obj.tag == "Enemy")
         {
             Debug.Log("Hit enemy!");
             obj.SendMessage("ApplyDamage", weaponDamage);
         }
     }
 }
 


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 Jwizard93 · Jul 05, 2017 at 10:28 PM

Are you sure movement() is being called? Maybe that if statement with the DialogManager is returning true.

Comment
Add comment · Show 4 · 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 Jwizard93 · Jul 05, 2017 at 10:32 PM 1
Share

Also you could just make the sword a child of the player then it's location would always just be an offset as it is relative to the player.

avatar image Jayby18 Jwizard93 · Jul 07, 2017 at 05:21 PM 0
Share

But if I make the sword a child of the player, colliders register the collision as the parent as well. Right now, if an enemy hits the sword, the enemy gets hit, but if the enemy hits the player's body, the player is hit. If I were to change it, the sword also counts as the player's body.

avatar image Jayby18 · Jul 07, 2017 at 05:21 PM 0
Share

I'll check that in a moment...

avatar image Jayby18 · Jul 08, 2017 at 05:46 PM 0
Share

That wasn't the case. It still didn't work. Inside $$anonymous$$ovement() I have a Debug.Log that is shown when GetAxis is returned above 0.

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

104 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

Related Questions

Problem between my player jump and player movement 1 Answer

Problem with 2D movment system C#. Keeps moving when no command is given 0 Answers

Using MoveTowards on button click 0 Answers

How do I stop momentum in 2D sidescroller? 1 Answer

Move player between 3 given points only by touching on the left/right side of the screen 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