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 21, 2011 at 01:30 PM · colliderpositioncontrollercollidewalls

Colliding for my character

I've made that code for my character, I can not add any colliding to it with other objects. Rigidbody wont effect it by it's default. Can anyone help me out?

The script:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 public class char_movement : MonoBehaviour 
 {
 
 
 
 public float moveSpeed = 15.0f;
 public float turnSpeed = 45.0f;
 public bool built = false;
 public float grove = 8.0f;
 public float velo = 1.0f;
 public float jumpStr = 10.0f;
 public bool onjump = false;
 public float timer = 1.0f;
     void Update()
     {
         CharacterController controller = GetComponent<CharacterController>();
        transform.position -= transform.TransformDirection(0,1.0f,0) * grove * velo * Time.deltaTime;
         
         if(Physics.Raycast(transform.position,-Vector3.up,1f))
         {
             built=true;
             Animateable();
         }
         else
         {
             built=false;
             animation.Play("jump");
         }
         
         if(onjump)
         {
             timer-= 3.0f * Time.deltaTime;
             transform.position+=transform.TransformDirection(0,2.0f,0)*jumpStr*Time.deltaTime;
             jumpStr-=0.7f;
             if(timer<=0.0)
             {
                 onjump=false;
                 timer=1.0f;
                 jumpStr=10.0f;
             }
         }
         if(built)
        {
           velo = 0.0f;
            if(Input.GetButtonDown("Jump"))
          {
              onjump=true;
          }
        }
        
        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); 
        
        
        if(!built)
            velo += 1.0f * Time.deltaTime;
        
        }
     
     
     //-------------------
     void Animateable()
     {
         if(Input.GetKey(KeyCode.W))
        {
            animation.Play("walk");
        }
         else if(Input.GetKey(KeyCode.S))
        {
             animation.Play("walkback");
        }
 
 
          else if(Input.GetKey(KeyCode.A))
        {
                        animation.Play("walk");
        }
 
 
         else if(Input.GetKey(KeyCode.D))
        {
                        animation.Play("walk");
        }
        else
        {
            animation.Play("idle");
        }
     }
     //--------------------
     
     
 }
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 aldonaletto · Jul 21, 2011 at 02:12 PM

For Character Controllers you must use OnControllerColliderHit - but it reports collisions with the ground all the time, so you may want to filter out these collisions. A simple way is to ignore collisions with normals > 45 degrees:

void OnControllerColliderHit(ControllerColliderHit hit){

 if (hit.normal.y < 0.707f) { // ignore collisions at the bottom
     // do whatever you want
 }

}

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
avatar image
0

Answer by benk0913 · Jul 21, 2011 at 03:23 PM

Many thanks, i'll try it out and report back if it worked!

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

Character Controller, changing collider's size? 0 Answers

Player falls through floor after 10-20 seconds and above certain resolution consistantly(Bug?) 5 Answers

isTouching() with multiple colliders 0 Answers

The problem with the change of position and colliders. 1 Answer

The appearance of some prefab appears somewhere else but their collider stays in the good place, that to make? 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