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 /
  • Help Room /
avatar image
0
Question by Big2 · Oct 11, 2015 at 10:54 AM · raycastingmovement script

Landing doesnt work

hi guys, i got a problem about my movement. i want to get the falling speed of the player, and a raycast telling me that the player is grounded. here is my script:

 public float speed = 1F;
 public float sprintSpeed = 3F;
 public float jumpSpeed = 7F;
 public float gravity = 20F;
 public float v;
 float RunCycleLegOffset = 0.2F;
 const float k_Half = 0.5f;
 float fallingspeed2;
 public Vector3 moveDirection = Vector3.zero;
 CharacterController cc;
 public Animator anim;

 // Use this for initialization
 void Start () {
     cc = GetComponent<CharacterController> ();
     anim = GetComponent<Animator> ();
 }

 void FixedUpdate () {
     float h = Input.GetAxis ("Horizontal");
     anim.SetFloat("Direction", h); 
     v = Input.GetAxis("Vertical");


     
 }
 // Update is called once per frame
 void Update () {

     float runCycle = Mathf.Repeat(anim.GetCurrentAnimatorStateInfo(0).normalizedTime + RunCycleLegOffset, 1);
     float jumpLeg = (runCycle < k_Half ? -1 : 1) * Input.GetAxis ("Vertical");
     //Debug.Log (jumpLeg);
     //======================================================================================================
     if (cc.isGrounded) 
     {
         moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
         moveDirection = transform.TransformDirection (moveDirection);
         moveDirection *= speed;
         anim.SetFloat("JumpLeg", jumpLeg);
         if (Input.GetButtonDown("Jump") && Input.GetAxis ("Vertical") >= 0)
         {
             moveDirection.y = jumpSpeed;
             anim.SetBool("Space", true);
         }
         else
         anim.SetBool("Space", false);
         if (Input.GetAxis ("Vertical") > 0.01 && Input.GetKey(KeyCode.LeftShift))
         {
             sprint();
             v += 1;
         }
     }
     moveDirection.y -= gravity * Time.deltaTime;
     cc.Move (moveDirection * Time.deltaTime);
     //=======================================================================================================
     if(!cc.isGrounded)
     {
         if(moveDirection.y < fallingspeed2)
         {
             fallingspeed2 = moveDirection.y;
         }
     }
     Debug.Log (fallingspeed2);
     float landing = (fallingspeed2 < -10 ? 1 : 0);
     //=======================================================================================================
     RaycastHit hit;
     float downraylength = cc.height / 2 + 0.02f;
     if(Physics.Raycast(transform.position, Vector3.down, out hit, downraylength))
     {
         anim.SetBool("isGrounded", true);
         Debug.Log ("grounded");
         fallingspeed2 = 0;
     }
     anim.SetBool("isGrounded", false);
     Debug.DrawRay (transform.position, transform.TransformDirection(Vector3.down) * downraylength, Color.green);
     //=======================================================================================================
     anim.SetFloat ("Landing", landing);
     anim.SetFloat ("Speed", v);
     float y = moveDirection.y;
     anim.SetFloat ("verticalSpeed", y);
     float fall = fallingspeed2;
     anim.SetFloat ("fallingSpeed", fall);
     Debug.Log (moveDirection.y);
     //========================================================================================================
     if(anim.GetCurrentAnimatorStateInfo(0).IsName("roll"))
     {
         moveDirection *= 0;
         fallingspeed2 = 0;
     }
 }


 void sprint () {
     moveDirection.z *= sprintSpeed;
     moveDirection.x *= sprintSpeed;
 }

its my whole movement script. i know its a bit messy

the speed parameter works, but the isgrounded mechanism doesnt seem to work. the player is grounded but when its in the animator, it doesnt work, and the player will forever stay in the falling animation even when its on the ground. i dont even know why so please help

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

0 Replies

· Add your reply
  • Sort: 

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

How to change direction of RayCast based on camera angle? 0 Answers

Raycast on left side and right side of the player 1 Answer

ball 3d movement, drag and release force. 0 Answers

I have a few truck driving forward automatically, but I want that when they come on their side that they stop moveing. 0 Answers

Diagonal movement is faster than horizontal or vertical, 2D top down, left click to move 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