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 Pulsar-Regions · Apr 03, 2013 at 10:04 PM · collisionmovementtransformrotategravity

Can't get gravity to work proberly (transform)

So I'm using a transform as a player with a camera following it. Gravity works fine at start, also when I hit the floor mostly. Sometimes the transform starts to rotate in these weird directions and suddenly my player can fly around rotating forever in the void. Here is a picture: alt text

As you can see the cube had begun to rotate and floating.

It's very random when it happens sometimes it works for just a while and then it screws up.

Here is my code:

 var speed : float = 5.00;
 var yVelocity : float = 10.00;
 var gravity : float = 5.00;
 var onFloor : boolean = false;
 
 function Update () {
     plyMove();
     
     if(onFloor == false) {
         transform.position.y -= gravity * Time.deltaTime;
     } else {
         return;
     }
 }
 
 function plyMove () {
     if(Input.GetKey("s")) {
         transform.Translate(-Vector3.forward * speed * Time.deltaTime);
     }
     
     if(Input.GetKey("w")) {
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
     }
     
     if(Input.GetKey("d")) {
         transform.Rotate(Vector3(0,15,0) * Time.deltaTime);
     }
     
     if(Input.GetKey("a")) {
         transform.Rotate(Vector3(0,-15,0) * Time.deltaTime);
     }
     
     if(Input.GetKeyDown("space")) {
         transform.position.y += yVelocity * Time.deltaTime;
     }
 }
 
 function OnCollisionEnter(theCollision : Collision) {
     if(theCollision.gameObject.tag == "floor") {
         onFloor = true;
     }
 }
 
 function OnCollisionExit(afterCollision : Collision) {
     if(onFloor == true) {
         onFloor = false;
     }
 }

also I believe it could be something about the rotation, but im not sure.

Comment
Add comment · Show 3
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 Unitraxx · Apr 03, 2013 at 10:14 PM 0
Share

Why would you build your own gravity mechanism?

avatar image Benproductions1 · Apr 03, 2013 at 10:19 PM 0
Share

You know Unity has both rigidbody physics and Character Controller Physics?

avatar image Garth-Smith · Apr 03, 2013 at 10:34 PM 0
Share

Program$$anonymous$$g a physics engine from scratch was a great learning experience for me. Then I threw up my hands and appreciated how much easier it was to use Unity's physics engine.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Garth-Smith · Apr 03, 2013 at 10:37 PM

Are you combining your own physics with some colliders? Maybe the built-in collisions in Unity are knocking your player around in weird directions when it hits the floor? Hitting the floor could cause your object to get some kind of momentum causing it to rotate forever or start moving on its own.

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
0

Answer by Pulsar-Regions · Apr 04, 2013 at 06:20 AM

I'm not sure what you mean, as you can see in my code I've combined my own physics with colliders. Basicly this is all the code I have in my project, except the camera that follow the cube.

Here is the camera code if needed: var target : Transform; var smooth : float = 400;

 function Update () {
     camMove();
 }
 
 function camMove () {
     transform.position.x = Mathf.Lerp(transform.position.x, target.position.x, Time.deltaTime * smooth);
 }
 

EDIT: I'll just use rigidbody Thanks for the answers

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

13 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

Related Questions

move camera when it collides with a trigger 1 Answer

How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 Answers

Object "crawling" over floor, how? 2 Answers

How to move a Game Object from a script not attached to it. 1 Answer

Character Transform shifting after colliding with objects 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