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 $$anonymous$$ · Jan 14, 2016 at 08:43 PM · collisionphysicscolliderscharacters

This wont work with a Character Controller.

Here is the problem. this works with a humanoid character wearing a rigid body and cap.collider but that sucks balls when it comes to getting any sort of proper gravity movement and detection.

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 
 public class HealthControl : MonoBehaviour
 {
     public Image healthBar;
     public float health;
     public GameObject restartDialog;
     
 
     void Start()
     {
         ShowRestartDialog(false);
     }
     void Update()
     {
         checkHealth();
     }
     void checkHealth()
     {
         healthBar.rectTransform.localScale = new Vector3(health / 100, healthBar.rectTransform.localScale.y, healthBar.rectTransform.localScale.z);
         if (health <= 0.0f)
         {
             ShowRestartDialog(true);
         }
     }
     void OnCollisionEnter(Collision other)
     {
         if (other.collider.CompareTag("Death"))
         {
             SubtractHealth(25.0f);
         }
         else if (other.collider.CompareTag("Life"))
         {
             AddHealth(25.0f);
         }
     }
     public void SubtractHealth(float amount)
     {
         if (health - amount < 0.0f)
         {
             health = 0.0f;
         }
         else
         {
             health -= amount;
         }
     }
     public void AddHealth(float amount)
     {
         if (health + amount > 100.0f)
         {
             health = 100.0f;
         }
         else
         {
             health += amount;
         }
     }
     public void ShowRestartDialog(bool c)
     {
         if (c)
         {
             Time.timeScale = 0.0f;
         }
         else
         {
             Time.timeScale = 1.0f;
         }
         restartDialog.SetActive(c);
     }
     public void Restart()
     {
        SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);
     }
 }

I tried this adaptation to player movement but loose not only the ability to jump, but the health script ceases to work, but i get all my other proper movement and gravity.

using UnityEngine;

 public class PlayerControl : MonoBehaviour
 {
     public Animator anim;
     public CharacterController  cc;
     public float pushPower = 2.0F;
     public float speed;
     public float inputH = 20f;
     public float inputV = 50f;
     private bool run;
     private bool jump;
     private object hit;
 
     void OnControllerColliderHit(ControllerColliderHit hit)
     {
         Rigidbody body = hit.collider.attachedRigidbody;
         if (body == null)
             return;
         if (hit.moveDirection.y < -0.3F)
             return;
 
         Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
         body.velocity = pushDir * pushPower;
     }
         // Use this for initialization
         void Start()
     {
         anim = GetComponent<Animator>();
         cc = GetComponent<CharacterController>();
         cc.detectCollisions = false;
         run = false;
         jump = false;
     }
 
     // Update is called once per frame
     void Update()
     {
        
         if (Input.GetKey(KeyCode.RightControl))
         {
             run = true;
         }
         else
         {
             run = false;
         }
 
         if (Input.GetKey(KeyCode.Space))
         {
           jump = true;
         }
         else
         {
           jump = false;
         }
         {
             CharacterController controller = GetComponent<CharacterController>();
             Vector3 horizontalVelocity = controller.velocity;
             horizontalVelocity = new Vector3(controller.velocity.x, 0, controller.velocity.z);
             float horizontalSpeed = horizontalVelocity.magnitude;
             float verticalSpeed = controller.velocity.y;
             float overallSpeed = controller.velocity.magnitude;
             if ((controller.collisionFlags & CollisionFlags.Above) != 0)
                 print("WTF");
         }
 
             inputH = Input.GetAxis("Horizontal");
             inputV = Input.GetAxis("Vertical");
        
         anim.SetFloat("inputH", inputH);
         anim.SetFloat("inputV", inputV);
         anim.SetBool("run", run);
         anim.SetBool("jump", jump);
 
         float moveX = inputH * 20f * Time.deltaTime;
         float moveZ = inputV * 50f * Time.deltaTime;
 
         if (moveZ <= 0f)
         {
             moveX = 0f;
         }
         else if (run)
         {
             moveX *= 6f;
             moveZ *= 6f;
 }
    
 }
 }

so i went back to thios setup and i get my jumping and movement but of course i get stuck jumping in place but to keep my character from getting knocked off into space or falling over, you HAVE TO HAVE the rotations of x, y, and z checked, and if you uncheck Y on position, yes you can fall, but at a freaking snail pace, and it is frankly garbage.

 using UnityEngine;
 
 
 public class PlayerControl : MonoBehaviour
 {
     public Animator anim;
     public Rigidbody Rb;
 
     public float speed;
     public float inputH = 20f;
     public float inputV = 50f;
     private bool run;
     private bool jump;
     
 
     // Use this for initialization
     void Start()
     {
         anim = GetComponent<Animator>();
         Rb = GetComponent<Rigidbody>();
         run = false;
         jump = false;
     }
 
     // Update is called once per frame
     void Update()
     {
        
         if (Input.GetKey(KeyCode.RightControl))
         {
             run = true;
         }
         else
         {
             run = false;
         }
 
         if (Input.GetKey(KeyCode.Space))
         {
           jump = true;
         }
         else
         {
           jump = false;
         }
 
         inputH = Input.GetAxis("Horizontal");
         inputV = Input.GetAxis("Vertical");
 
         anim.SetFloat("inputH", inputH);
         anim.SetFloat("inputV", inputV);
         anim.SetBool("run", run);
         anim.SetBool("jump", jump);
 
         float moveX = inputH * 20f * Time.deltaTime;
         float moveZ = inputV * 50f * Time.deltaTime;
 
         if (moveZ <= 0f)
         {
             moveX = 0f;
         }
         else if (run)
         {
             moveX *= 6f;
             moveZ *= 6f;
         }
         Rb.velocity = new Vector3(moveX, 0f, moveZ * speed);
        
     }
 }

Please do not tell me "what number line" i need to put what where, because presently I am using visual studio and it isn't allowing me to use the "line number" views. If you know what line string to put in where that I am missing, simply specify copy these codes, and do the ///Here is the change/// so i can see what i was doing incorrectly.

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

50 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

Related Questions

Question on Physics.Simulate Behavior 0 Answers

Why is my trigger collider acting like a normal collider? 1 Answer

Car will only turn right (& ignored collders) 0 Answers

Block Issues 1 Answer

Colliders Don't Work no matter what 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