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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by TheCRRRG · Mar 23, 2013 at 06:23 PM · collisionmovement

Deteching collsions when jumping when not using a rigidbody

Hi, I've recently started to mess around with Unity.

Atm I'm trying to create a simple movement script in javascript using a cube as the object I want to move. I don't want to make it a rigidbody however I cannot work out how to make it detect the floor when it "jumps".

Any feedback will be very useful, thanks.

 #pragma strict
 //movement variables 
 var moveSpeed: int = 2;
 var backSpeed: int = 1;
 var runSpeed:  int = 3;
 var rotationSpeed: int = 5;
 
 //variables to do with jumping
 var jumpHeight: int = 5;
 var inJump: boolean = false;
 var gravity: float = 20.0;
 
 
 var move: Vector3;
 
 
 
 
 function Start () {
 
 }
 
 function Update () {
 
     
     // moving forward
     if(Input.GetKey(KeyCode.W)){
      move = Vector3.forward * moveSpeed;
      transform.Translate(move * Time.deltaTime);
           } 
      
      // jumps, makes it know its in the air
      if(Input.GetKeyDown(KeyCode.Space)&& inJump == false){
    
       move = new Vector3(0,jumpHeight,0);
       transform.Translate(move * Time.deltaTime);
       inJump = true;
   
      }
      //if it is currently jumping, apply gravity to it
      if (inJump == true){
         move.y -= gravity * Time.deltaTime;
          transform.Translate(move * Time.deltaTime);
      }
      if (//NEEDS SOME SORT OF COLLISON DETECTION HERE ) {
      inJump = false;
       } 
      
      
 
 }
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 robertbu · Mar 23, 2013 at 07:21 PM

You have a number of choices:

  • If you are using a plane as your floor and are not worrying about landing on other objects, you can just use the 'y' position of your block. This obviously won't work if you are jumping from platform to platform.

  • You can so a Physics.Raycast(). down to determine how far below you is any object, and use what it returns as your floor. The issue here is that you typically only test the position directly below the center of the cube. So you will not detect a glancing hit on another object below you.

  • A Physics.SphereCast() may be what you are looking for. Since a sphere will not exactly match the shape of your object, it is not a perfect solution.

And there are other solutions. Below I made changes to your code to use a SphereCast(). There are a significant number of changes.

 #pragma strict
 //movement variables 
 var moveSpeed: int = 2;
 var backSpeed: int = 1;
 var runSpeed:  int = 3;
 var rotationSpeed: int = 5;
 var objectHeight = 0.49;  // Distance between center point of the object and the ground.
 var objectSize   = 0.5;  // Raidus of the sphere cast 
  
 //variables to do with jumping
 var jumpHeight: int = 5;
 var inJump: boolean = false;
 var gravity: float = 20.0;
  
 var move: Vector3;
 var ground = 0.0;  
 
 function Update () {
     // moving forward
     if(Input.GetKey(KeyCode.W)){
      move = Vector3.forward * moveSpeed;
      transform.Translate(move * Time.deltaTime);
          } 
  
      // jumps, makes it know its in the air
      if(Input.GetKeyDown(KeyCode.Space)&& inJump == false){
  
       move = new Vector3(0,jumpHeight,0);
       transform.Translate(move * Time.deltaTime);
       inJump = true;
  
      }
      //if it is currently jumping, apply gravity to it
      if (inJump == true){
         move.y -= gravity * Time.deltaTime;
         transform.Translate(move * Time.deltaTime);
      }
      
      var hit : RaycastHit;
      if (Physics.SphereCast(transform.position, objectSize, Vector3.down, hit)) {
          //Debug.Log("-->"+hit.collider.name);
          ground = hit.point.y + objectSize;
      }
     
      if (ground >= transform.position.y){
          inJump = false;
          transform.position.y = ground;
       } 
 }

 


[3]: http://docs.unity3d.com/Documentation/Components/Layers.html

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 TheCRRRG · Mar 23, 2013 at 08:22 PM

Cheers will let you know if it works :)

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

10 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

Related Questions

How to move multiple dynamic gameobjects with one moving platform which is controlled by the player 0 Answers

How do I move this rigidbody? 1 Answer

How to get character movement done with box collider 2d? 2 Answers

Transforming position for different colliders for repeating background? 0 Answers

How to stop a character from moving out of player's control? 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