Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 nasby321 · Sep 03, 2013 at 10:24 PM · 2dplayerjumpsidescrollercrouch

Help with player script.

I have been working on a crouch function for my 2d side scroller. It works,but it stops the attack function from working. Also I have tried to add jumping in there, but I couldn't figure it out. Could some one please help me?

 using UnityEngine;
 using System.Collections;
 using SpriteFactory;
     
 public class player : MonoBehaviour {
     
     
     
     public float speed = 1.0f;
     public float jump = 10.0f;
     public float gravity = 10.0f;
 
         
     private Transform thisTransform;
     private Sprite sprite;
     private bool flipped;
 
     // Use this for initialization
     void Awake () {
         thisTransform = transform;
         sprite = (Sprite)GetComponent(typeof(Sprite));
     
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 moveVector = new Vector3(0,0,0);
         bool moving = false;
         
         
         
     if(Input.GetKey(KeyCode.A)){
             moveVector.x -= 1.0f;
             moving = true;
         
     }
         
         if(Input.GetKey(KeyCode.D)){
             moveVector.x += 1.0f;
             moving = true;
             
             
         }
         
 
         if(Input.GetKey(KeyCode.Mouse0)){
             Attack();
         } else {
             if(!IsAttacking()) {
                 if(moving) {
                     Move(moveVector);
                 } else {
                     Stand();
             }
         }
         }
         if(Input.GetKey(KeyCode.S)){
             Crouch();
         } else {
             if(!IsCrouching()) {
                 if(moving) {
                     Move(moveVector);
                 } else {
             Stand();
     }
     }
         }
     }
         
     
     
 
     
     private void Stand() {
         sprite.Stop();
     }
     
     
     private void Move(Vector3 moveVector) {
         FlipSprite(moveVector.x);
         moveVector *= speed * Time.deltaTime;
         thisTransform.position = thisTransform.position + moveVector;
         sprite.Play("walk");
         
         
         
     
     }
     
     private void FlipSprite(float x) {
         if(x == 0.0f) return;
         
         float xDir = Mathf.Sign(x);
         if(!flipped && xDir < 0.0f) {
             flipped = true;
             sprite.SetFlippedState(true, false);
         } else if(flipped&& xDir > 0.0f) {
             flipped = false;
               sprite.SetFlippedState(false, false);
         }
     }
     
     private void Attack() {
         if(IsAttacking()) return;
         
         sprite.Play("Sword");
     }
     
     private bool IsAttacking() {
         if(sprite.IsAnimationPlaying("Sword")) return false;
         return false;
     
 
 }
     
     private void Crouch() {
         if(IsCrouching()) return;
         
         sprite.Play("crouch");
     }
     
     private bool IsCrouching() {
         if(sprite.IsAnimationPlaying("crouch")) return false;
         return false;
     
 
     
     
 
     
         
     
 }            
         
     
     
 
 
 }
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
1
Best Answer

Answer by ShadoX · Sep 05, 2013 at 10:18 AM

The problem seems to be with your if:

   if(Input.GetKey(KeyCode.S)){
      Crouch();
    } else {//problem starts here
      if(!IsCrouching()) {
       if(moving) {
           Move(moveVector);
       } else {
      Stand();
 }

You check if the player is pressing "s" and if he is - the char crouches , if he isn't - you move (if moving == true).. you probably want to move the following part outside the "else" part

  if(moving) {
      Move(moveVector);
  }
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 nasby321 · Sep 05, 2013 at 11:47 AM 0
Share

That script now allows me to attack with the crouch added in, but I don't want him to move while crouched is there a way to do that?

avatar image ShadoX · Sep 05, 2013 at 12:34 PM 1
Share

Sorry, can't believe I missed that part.. in that case please ignore what I said about the if. Try having a look at http://answers.unity3d.com/questions/160848/how-do-i-run-two-animations-at-once-on-the-same-ga.html Initially I thought that you might want to handle the player input differently, but I guess that animation blending also should work.

avatar image nasby321 · Sep 05, 2013 at 08:22 PM 0
Share

Thanks! That helped!

avatar image Sajidfarooq · Sep 05, 2013 at 08:31 PM 0
Share

@nasby321: If it solved your problem, then don't forget to mark this question as "answered".

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

Enemy follows the only player in rotation 2 Answers

Why do I double jump? This isn't supposed to happen... 2 Answers

Player wont jump (visual script),2d player does not jump (visual script) 1 Answer

2D Level Design (programmatically or not) 1 Answer

Need help with 2D Attack, attacking in every direction 0 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