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
0
Question by benk0913 · Jul 17, 2011 at 03:34 PM · collisioncharactergrounded

Character is not grounding.

HELP!

Ive coded that :

using UnityEngine; using System.Collections;

 [RequireComponent(typeof(CharacterController))]
 public class char_movement : MonoBehaviour 
 {
 
     
 
 public float moveSpeed = 25.0f;
 public float turnSpeed = 25.0f;
 public bool gro = false;
 public float grove = 8.0f;
 public float velo = 1.0f;
     
     void Update()
     {
         CharacterController controller = GetComponent<CharacterController>();
                  if (controller.isGrounded)
        {
         //sets grounded
         gro = true;
         print("Touched");
        }
         transform.position -= transform.TransformDirection(0,1.0f,0) * grove * velo * Time.deltaTime;
         // If the char hits the ground
 
     //If it's grounded
            if(gro)
        {
         velo = 0.0f;
         Movement();
        }
        if(!gro)
        {
            velo += 1.0f * Time.deltaTime;
        }
     }
    
    
    void Movement()
    {
        if(Input.GetKey(KeyCode.W))
        {
            transform.position += transform.TransformDirection(0,0,1.0f) * moveSpeed * Time.deltaTime;
        }
         if(Input.GetKey(KeyCode.S))
        {
            transform.position += -transform.TransformDirection(0,0,1.0f) * (moveSpeed-10.0f) * Time.deltaTime;
        }
 
        
          if(Input.GetKey(KeyCode.A))
        {
              transform.Rotate(Vector3.up * -turnSpeed * Time.deltaTime);
        }
        
               
          if(Input.GetKey(KeyCode.D))
        {
             transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime); 
         }
 
    }
    
 }<code>

The character that ive made falls down as it needs to but it never collides the ground or reacts to the collisions below it.

Can anyone help me out?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DavidDebnar · Jul 17, 2011 at 03:47 PM

Place it anywhere.. Of you have a collider and your character moves downwards towards the ground, this will work :).

OnCollisionStay() { grounded = true; }

OnCollisionExit() { grounded = false; }

--David

Comment
Add comment · Show 1 · 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 DavidDebnar · Jul 17, 2011 at 03:48 PM 0
Share

It's in JavaScript, but I'm sure, that you can rewrite it into C#.

avatar image
0

Answer by benk0913 · Jul 17, 2011 at 03:56 PM

David, It still doesn't works. Heres the new code

using UnityEngine; using System.Collections;

[RequireComponent(typeof(CharacterController))] public class char_movement : MonoBehaviour {

public float moveSpeed = 25.0f; public float turnSpeed = 25.0f; public bool gro = false; public float grove = 8.0f; public float velo = 1.0f;

 void Update()
 {
     CharacterController controller = GetComponent<CharacterController>();
     transform.position -= transform.TransformDirection(0,1.0f,0) * grove * velo * Time.deltaTime;
     // If the char hits the ground

 //If it's grounded
        if(gro)
    {
     velo = 0.0f;
     Movement();
    }
    if(!gro)
    {
        velo += 1.0f * Time.deltaTime;
    }
 }
 // Davids adding
   
     

void OnCollisionStay() { gro = true; print("Touched"); }

void OnCollisionExit() { gro= false; }

// __

void Movement() { if(Input.GetKey(KeyCode.W)) { transform.position += transform.TransformDirection(0,0,1.0f) moveSpeed Time.deltaTime; } if(Input.GetKey(KeyCode.S)) { transform.position += -transform.TransformDirection(0,0,1.0f) (moveSpeed-10.0f) Time.deltaTime; }

      if(Input.GetKey(KeyCode.A))
    {
          transform.Rotate(Vector3.up * -turnSpeed * Time.deltaTime);
    }
    
           
      if(Input.GetKey(KeyCode.D))
    {
         transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime); 
     }

}

}

Comment
Add comment · Show 1 · 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 DavidDebnar · Jul 17, 2011 at 09:18 PM 0
Share

Which part isn't working? I think it's the grounded one, but to be sure ;). So are you writing the script on your own, or just making it from some template? And is it using rigidbody or the character controller?

And please post your answers and comments, with the comments function, so it's not very messy, and there aren't 5 answers, because than the answerers may think, that it's already answered and the creator of the ask just forgot to close it, if you wan't answers, clean this up ;).

  • $$anonymous$$

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Grounded Only on One Layer 1 Answer

Character doesn't collide with trees in terrain 4 Answers

Why can't my character jump 2 Answers

OnCollisionEnter Does NOT work for the player - But works flawlessly for other GameObjects 0 Answers

Character Collision isGrounded and Walls 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