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 LeightonW87 · Sep 01, 2014 at 11:02 PM · javascriptforceplayer movementball

Help with player movement and adding force to a ball.

Hi all, I'm new to unity and kind of new to a degree to programing, I've been looking at tutorials and guides to get the movement in my game working somewhat but until recently I've encountered an issue where the player will only walk about two steps then will stop, then he will become erratic until I let go and press again.

Could anyone help me with this as the movement is far from intended, also are there anyways of adding force to a ball in the direction the player is moving but preventing the player from being affected by the collision.

My code is below.

 import UnityEngine;
 import System.Collections;
 import UnityEngine.Animator;
 
 var LeftButton : String;
 var RightButton : String;
 var ForwardButton : String;
 var BackButton : String;
 var RunButton : String; 
 var AttackButton : String;
 
 var Dance : String;
 var Jump : String;
 
 var Forward :  Vector3;
 var Reverse : Vector3;
 
 var DT : double;
 
 var LeftRotation : float;
 var LeftForwardRotation : float;
 var ForwardRotation : float;
 var ForwardRightRotation : float;
 var RightRotation : float;
 var RightBackRotation : float;
 var BackRotation : float;
 var LeftBackRotation : float;
 var JumpHeight : float;
 
 var Suspended : boolean;
 var MoveSpeed : double;
 var isWalking : boolean;
 var isFalling : boolean;
 var isJumping : boolean;
 var isDancing : boolean;
 var isStanding : boolean;
 var directionsPressed : boolean;
 
 var anim : Animator;
 var cam : Camera;
 
 function Start () {
 
  anim = GetComponent("Animator");
 
     ForwardButton = "w";
     LeftButton = "a";
     RightButton = "d";
     BackButton = "s";
     RunButton = "u";
     Dance = "k";
     AttackButton = "j";
     
     Forward = new Vector3 (0, 0, -2);
     Reverse = new Vector3 (0, 0, 2);
     JumpHeight = 200;
     
     DT = Time.deltaTime;
     //45 Degree Angles
     
     ForwardRotation = 0;
     ForwardRightRotation = 45;
     RightRotation = 90;
     RightBackRotation = 135;
     BackRotation = 180;
     LeftBackRotation = 225;
     LeftRotation = 270;
     LeftForwardRotation = 315;
 
     Suspended = false;
     MoveSpeed = 20.0;
 }
 
 function Update () {
 
     if (Suspended)
         {
             return;
         }
 
 
 //-----------------------------------------------------------------
 
     if (Input.GetButton(RunButton))
     {
         MoveSpeed = 4;
     }
     else
     {
         MoveSpeed = 2;
     }
 
 if (directionsPressed == true) {
     
     if (Input.GetButton(ForwardButton))
     {
         transform.rotation.eulerAngles.y = ForwardRotation - Camera.current.transform.rotation.eulerAngles.y;
         transform.Translate(Forward * MoveSpeed * DT);
         isWalking = true;
     }
     
     if (Input.GetButton(BackButton))
     {
         transform.rotation.eulerAngles.y = BackRotation - Camera.current.transform.rotation.eulerAngles.y;
         transform.Translate(Forward * MoveSpeed * DT);
         isWalking = true;
     }
 
     if (Input.GetButton(LeftButton))
     {
         transform.rotation.eulerAngles.y = LeftRotation - Camera.current.transform.rotation.eulerAngles.y;
         transform.Translate(Forward * MoveSpeed * DT);
         isWalking = true;
     }
     
     if (Input.GetButton(RightButton))
     {
         transform.rotation.eulerAngles.y = RightRotation - Camera.current.transform.rotation.eulerAngles.y;
         transform.Translate(Forward * MoveSpeed * DT);
         isWalking = true;
     }
     
     if (Input.GetButton(LeftButton) && (Input.GetButton(ForwardButton)))
     {
         transform.rotation.eulerAngles.y = LeftForwardRotation - Camera.current.transform.rotation.eulerAngles.y;
         transform.Translate(Forward * MoveSpeed * DT / 4);
         isWalking = true;
     }
     
     if (Input.GetButton(RightButton) && (Input.GetButton(ForwardButton)))
     {
         transform.rotation.eulerAngles.y = ForwardRightRotation - Camera.current.transform.rotation.eulerAngles.y;
         transform.Translate(Forward * MoveSpeed * DT / 4);
         isWalking = true;
     }
     
     if (Input.GetButton(RightButton) && (Input.GetButton(BackButton)))
     {
         transform.rotation.eulerAngles.y = RightBackRotation - Camera.current.transform.rotation.eulerAngles.y;
         transform.Translate(Forward *MoveSpeed * DT / 4);
         isWalking = true;    
     }
     
     if (Input.GetButton(LeftButton) && (Input.GetButton(BackButton)))
     {
         transform.rotation.eulerAngles.y = LeftBackRotation - Camera.current.transform.rotation.eulerAngles.y;
         transform.Translate(Forward * MoveSpeed * DT / 4);
         isWalking = true;
     }
 }
     
     
     
 //Check if Directions are being pressed.
     
     if (Input.GetButton(LeftButton)) 
         {
             //Debug.Log("Left Pressed");
             directionsPressed = true;
         }
     
     if (Input.GetButton(RightButton))
         {
             //Debug.Log("Right Pressed");
             directionsPressed = true;
         }
         
     if (Input.GetButton(ForwardButton))
         {
             directionsPressed = true;
         }
     
     if (Input.GetButton(BackButton))
         {
             directionsPressed = true;
         }
     
 //Check if Directions are being let go.
     
     if (Input.GetButtonUp(LeftButton)) 
         {
             directionsPressed = false;
             isWalking = false;
         }
     
     if (Input.GetButtonUp(RightButton))
         {
             directionsPressed = false;
             isWalking = false;
         }
         
     if (Input.GetButtonUp(ForwardButton))
         {
             directionsPressed = false;
             isWalking = false;
         }
     
     if (Input.GetButtonUp(BackButton))
         {
             directionsPressed = false;
             isWalking = false;
         }
     
 //----------------------------------------------------------------
     
     
     if (directionsPressed == false && isJumping == false)
     {
             //Debug.Log("I'm now standing.");
             //animation.Play("Standing_Still");
             anim.SetInteger("AnimationtoPlay", 0); 
             isStanding = true;
     }
     
     /*        
     if (isJumping == false && isWalking == true)
     {
             Debug.Log("I should be walking now.");
             anim.SetInteger("AnimationtoPlay", 2); 
             
         animation.Stop("FALLING");
             isStanding = false;
             isJumping = false;
             isDancing = false;
             isWalking = true;
     }
     */
     
     /*
     if (Input.GetButton(Dance))
         {
             Debug.Log("Look at me dance!");
             anim.SetInteger("AnimationtoPlay", 3); 
             //animation.Stop("Walking");
             //animation.Stop("Jumping");
             //animation.Stop("Standing_Still");
             //animation.Play("Dance");
             isStanding = false;
             isWalking = false;
             isJumping = false;
             isDancing = true;
         }
     */    
         
     if (isJumping == false && (Input.GetButtonDown("Jump")))
     {
              Debug.Log("I should be jumping up now.");
 
             rigidbody.AddForce(Vector3.up *JumpHeight);
             animation.Stop("Walking");
             anim.SetInteger("AnimationtoPlay", 1); 
             isStanding = false;
             isDancing = false;
             isWalking = false;
             isJumping = true;
     }
     
     if (Input.GetButton("AttackButton"))
     {
         anim.SetInteger("AnimationtoPlay", 4);
     }
 }
 
 
 function OnCollisionEnter(theCollision : Collision)
 {
      if (theCollision.gameObject.name == "Field")
      {
          animation.Stop("Jumping");
          animation.Stop("FALLING");
          isJumping = false;
          isFalling = false;
          isStanding = true;
          isDancing = false;
          
          Debug.Log("I landed on the floor");
      }
      
      /*
      else if (theCollision.gameObject.name == "Single Block")
      {
          Debug.Log("Hit the wall");
      }
      */
 }
 
 function SetSuspension(setting : boolean)
 {
     Suspended = setting;
 }
 
 

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 marsfan · Sep 01, 2014 at 11:29 PM

Adding force is pretty easy, just us the rigid body force section. If you need to add force at a certain part in the game, use rigidbody.addforce() in the code. I am not sure if that will use global or local orientation, but you can find that on the code database on this website.

As for the walking persons, I am not sure, I am Also a basic user.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Player sticks to ground occasionally after trying to jump 0 Answers

How can i make a ball jump in a curve from one place to another and vice versa. Like juggling a ball ! 0 Answers

I need help converting this script from zilch to js 0 Answers

(Noob) How to stop force? (golfball) 3 Answers

Ball Rotating Help 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