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 JuanseCoello · Jun 09, 2013 at 10:20 PM · collisioncharactercontroller

How can i add a collision detection for a character controller?

The problem is that my scripts work fine, except that the character walks trhough geometry even though it has a character controller attachhed to it. I have two scripts. One named TP_Motor, and the other TP_Controller. Here are the scripts: TP_Motor

using UnityEngine; using System.Collections;

public class TP_Motor : MonoBehaviour { public static TP_Motor Instance;

 public float movementSpeed = 10;
 public float backwardSpeed = 5;
 public float Gravity = 40f;
 public float TerminalVelocity = 20f;

 
 private Vector3 slideDirection;
 
 public float turningSpeed = 60;
 public float MoveSpeed = 10f;
 public float JumpSpeed = 14f;
 public Vector3 MoveVector { get; set; }
 public float VerticalVelocity { get; set; }
 

 void Update() 
 {
     Instance = this;
 }
 
 public void UpdateMotor() 
 {
      
     
     SnapAlignCharacterWithCamera();
     Moveandrotate();        
     ProcessMotion();
 }
 
 void Moveandrotate() // Esto hace que el jugador se mueva y rote.
     
     {
     float horizontal = Input.GetAxis("Horizontal") * turningSpeed * Time.deltaTime;
     transform.Rotate(0, horizontal, 0);
     
     
     float vertical = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
     transform.Translate(0, 0, vertical);
 }
 
 void ProcessMotion()
 {
     // Transform MoveVector to World Space
     MoveVector = transform.TransformDirection(MoveVector);
     
     // Normalize MoveVector if Magnitude > 1
     if (MoveVector.magnitude > 1)
         MoveVector = Vector3.Normalize(MoveVector);
     
     // Multiply MoveVector by MoveSpeed Esto hace que se mueva de derecha a izquierda
     MoveVector *= MoveSpeed;
     
     // Reapply VerticalVelocity MoveVector.y
         MoveVector = new Vector3(MoveVector.x, VerticalVelocity , MoveVector.z);
     
     // ApplyGravity
     ApplyGravity();
     
     // Move Character in World Space Esto tambien hace que se mueva de derecha a izquierda
     TP_Controller.CharacterController.Move(MoveVector * Time.deltaTime);
 }
 
 void ApplyGravity()
 {
     if (MoveVector.y > -TerminalVelocity)
         MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
     
     if(TP_Controller.CharacterController.isGrounded && MoveVector.y < -1)
         MoveVector = new Vector3(MoveVector.x, -1 , MoveVector.z);
         
 }
 
 public void Jump()
 {
     if (TP_Controller.CharacterController.isGrounded)
         VerticalVelocity = JumpSpeed;
 }
 
 void SnapAlignCharacterWithCamera()
 {
 
 }

}

AND THE TP_Controller

using UnityEngine; using System.Collections;

public class TP_Controller : MonoBehaviour { public static CharacterController CharacterController; public static TP_Controller Instance; public float MoveSpeed = 10f;

 void Awake() 
 {
     CharacterController = GetComponent();
     Instance = this;
 }
 
 void Update() 
 {
     if(Camera.mainCamera == null)
         return;
     
     GetLocomotionInput();
     HandleActionInput();
     
     TP_Motor.Instance.UpdateMotor();
 }
 
 
 void GetLocomotionInput()
 {
     var deadZone = 0.1f;
     
     TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y; // Gravedad
     
     TP_Motor.Instance.MoveVector = Vector3.zero;
     
     if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
         TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
     
 }
 
 void HandleActionInput() // Esto hace junto con jump que se accione con un boton el Jump
 {
     if (Input.GetButton("Jump"))
     {
         Jump ();
     }
 }
 
 void Jump()
 {
     TP_Motor.Instance.Jump ();
 }

}

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 InfiniBuzz · Jun 09, 2013 at 10:38 PM

Hi

make sure your walls or whatever object the character should collide with have colliders (and rigidbodies where needed) too.

Comment
Add comment · Show 2 · 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 JuanseCoello · Jun 09, 2013 at 11:27 PM 0
Share

they do have colliders, the script has some problem, because if i use other scripts there is no problem at all.

avatar image InfiniBuzz · Jun 09, 2013 at 11:33 PM 0
Share

sorry for posting this as an answer not as a comment. I'll have a closer look

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

15 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

Related Questions

Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer

Character Controller meets Rigidbody 1 Answer

Problem with character controller and collisions 1 Answer

Collision CharacterController vs CharacterController 2 Answers

NPCs fall through hills in terrain 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