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 GetUpKidAK · May 13, 2013 at 06:44 PM · collisionphysicsrigidbody

Character rigidbody collisions

Hi,

I'm fairly sure I'm having a complete brain-freeze about this, but I'm struggling to get my head around rigidbodies and general physics interactions.

I'm currently moving my player character around with the code below, but I can't seem to get the characters to collide with any objects.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour
 {
     public    float        moveSpeed    =    10f;
 
     private    Vector3        lookTarget;
     
     void FixedUpdate()
     {
         // Forward and sideways movement
         float transH = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
         float transV = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
         
         transform.Translate(transH, 0, transV, Space.World);
         
         // Mouselook
         Plane playerPlane = new Plane(Vector3.up, transform.position);
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         float hitdist = 0.0f;
         
         if (playerPlane.Raycast(ray, out hitdist))
         {
             var targetPoint = ray.GetPoint(hitdist);
             var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
             
             transform.rotation = targetRotation;
         }
     }
 }

There is a rigidbody attached to the player, along with a capsule collider (the player is currently just a capsule primitive anyway) and there is a box collider on the basic wall I've been trying this on.

My understanding was that 'Use gravity' should be disabled if the object is being moved by its transform, but my character passes through any object unless this is ticked - I've attempted various combinations ('Is Kinematic' ticked and unticked, a rigidbody attached to the wall, etc.), but it's got to the point where I've got completely lost.

Any help would be appreciated!

Comment
Add comment · Show 2
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 buxton4life · May 13, 2013 at 07:52 PM 0
Share

Sounds like you have "trigger" ticked in the inspector that would prevent collisions my friend.

avatar image ExTheSea · May 13, 2013 at 08:20 PM 0
Share

From what i know if you use Transform.Translate or change the transform.position on a gameobject it will not react to collisions. If you have a rigidbody attached you should use Rigidbody.AddForce. Google for it and you should find several rigidbody movement scripts.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DESTRUKTORR · May 13, 2013 at 08:44 PM

If you manually change positions via the transform, you will be able to pass through objects. Telling it to position itself in the middle of another collider will trigger a collision, but will not force itself out from the other collider. You will need to use either rigidbody.AddForce() or rigidbody.velocity to govern speed or direction of movement, if you want collisions to be included in the physics calculations.

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 GetUpKidAK · May 29, 2013 at 12:13 PM 0
Share

Hi, sorry for the late reply!

$$anonymous$$y problem with using AddForce() (or AddRelativeForce()) is that there doesn't seem to be any fine control over the movement. Also, as this is a player movement script, it is being called every FixedUpdate(), so the player velocity is increasing over time.

I know I can clamp the velocity, but I still have issues with the character slowing to a stop after the input is released.

avatar image DESTRUKTORR · Jul 28, 2013 at 06:13 PM 0
Share
 if(!Input.GetButton ("WhateverYouChose"))
     if(yourDesiredObject.rigidbody.velocity.sqr$$anonymous$$agnitude > 0.0001f)
         yourDesiredObject.rigidbody.velocity *= 0.95f;// Any literal float between 0 and 1 (not including 1.0f) will work, here.
     else
         yourDesiredObject.rigidbody.velocity = Vector3.zero;

All AddForce and AddRelativeForce do is add to the unit's immediate velocity, based on its mass.

Another option would be to change the physic material on your object's collider to one with a greater amount of friction on it.

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

17 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

Related Questions

Keep Horizontal Momentum after Jump 2 Answers

Collision.impulse = 0 in OnCollisionStay Kinematic Static collision pair after Update 0 Answers

Ping Pong bat is not detecting the collision with the wall 1 Answer

Help with character controller collision... 2 Answers

physics culling mask? 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