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 MilkX5 · Jan 17, 2014 at 03:36 PM · collision3dcharactercontrollertranslate

3D Collision with Transform.Translate

I'm using transform.Translate as well as a Character Controller for my 3D player and a rotating camera controlled by the mouse. The movement for the character works fine as long as I use transform.Translate for the movement and Character.Move for the jumping. The CharacterController doesn't work well for movement because it isn't relative to the camera. Is there anyway I can make collisions with the world while using transform.Translate or is there another solution to movement while still being relative to the camera?

Here is my code so far

 //Player 1 
 var gravity    : float     = 20.0;                                                            //set gravity variab;e
 var hopValue : float     = 0.0;                                                            
 var moveSpeed : float     = 5.0;                                                            //set movement speed
 var jumpSpeed : float    = 10.0;                                                            //set jump height
 var velocity : Vector3    = Vector3.zero;                                                    //set velocity variable for 3d movement
 var FPcamera : GameObject;                                                                //define first person camera
 var TPcamera : GameObject;                                                                //define third person camera
 var startPos : Transform;                                                                //define start position for player
 
 
 function Start(){                                                                        //starting function called when player is instantiated
     FPcamera.active = true;                                                                    //activate first person camera
     TPcamera.active = false;                                                                //deactivate third person camera
 }
 function Update () {                                                                    //constantly updated function called every frame
     var controller : CharacterController = GetComponent(CharacterController);                //define character controller from the collision body
     
     var transLR : float = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;            //define left and right movement by the horizontal axis keys
     var transFB : float = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;            //define forward and backward movement by the vertical axis keys
     
     if(controller.isGrounded){                                                                //if player is on the ground
         if(Input.GetButton("Jump")){                                                            //if pressing the jump button
             velocity.y = jumpSpeed;                                                                    //move up by jump speed
         }
     }
     transform.Translate(transLR, 0, transFB);                                                //move by the left, right, forward, and backward movements
     
     velocity.y -= gravity * Time.deltaTime;                                                    //apply gravity
     controller.Move(velocity * Time.deltaTime);                                                //always move the character controller
     
     
     //cameras
     if(Input.GetKeyDown("q")){                                                                //if pressing the q key
         FPcamera.active = !FPcamera.active;                                                            //toggle first person camera
         TPcamera.active = !TPcamera.active;                                                            //toggle third person camera
     }
     transform.Rotate(0,Input.GetAxis("Mouse X") * 5, 0);                                    //rotate body based on mouse x axis    
     Screen.lockCursor = true;                                                                //lock the cursor on the screen
     
 }
 function OnTriggerEnter(other : Collider){                                            //function for entering object with a trigger
     if(other.tag == "kill_Zone"){                                                        //if the object is a kill zone
         this.transform.position = startPos.transform.position;                                //restart position to the start position
     }
 }
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

18 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

Related Questions

Move Objects with Collision Without Impacting Frame Rate 0 Answers

My player collision is not accurate/precise enough 2 Answers

When using a character controller for the player, can other colliders be used alongside the character controller? 1 Answer

CharacterController & Particles 1 Answer

Problem with rigidbodys and box collider 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