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 xwolfhunter · Jun 26, 2012 at 10:50 PM · hitwall

Making My Character Stop When It Hits A Wall

So, guys. I just started a project this morning. On all my little fun experiments, I've used a presupplied character controller. This time, I made my own, and here's how I did it: I made a simple movement script, attached it to a box, and then parented it to the main camera. Simple, right? Just in case it's necessary, here's the script I wrote:

  > public var speed = 5;
  function Start () {
  }
  function Update () {
      var newPosition : Vector3 = transform.position;
      if (Input.GetKey (KeyCode.W)){
      newPosition.z += speed * Time.deltaTime;
      }
      if (Input.GetKey (KeyCode.S)){
      newPosition.z -= speed * Time.deltaTime;
      }
      if (Input.GetKey (KeyCode.A)){
      newPosition.x -= speed * Time.deltaTime;
      }
      if (Input.GetKey (KeyCode.D)){
      newPosition.x += speed * Time.deltaTime;
      }
      transform.position = newPosition;
  }

I then made boxes and a wall. Their positions and rotations are locked on all axis, and the character's position is locked on the y axis. The character's rotations are locked on all axis as well.

So, I made them all rigidbodies to attempt to make it so that that the character would stop when it hit something. It worked, to the extent that the character could not pass through boxes. However, it gained permanent momentum in the opposite direction that it encountered the rigidbody at. When it came to a wall, the character was flung through it and gained tons of permanent momentum.

I'm not sure where to go from here. I did a bit of research, and saw somewhere to check the "Is Kinematic" button. However, all this did was make the character pass straight through the other boxes.

If you want to actually see this happen, go here:

http://xoraterra.blogspot.com/2012/06/welcome-to-blog.html

Thanks for any help, guys!

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
2

Answer by torrente · Jun 27, 2012 at 02:37 AM

It looks like you have three systems fighting there... you are moving the object with values that manipulate the positioning of transform and not the rigid body. It works, but think of it as the rigid simply going where the transform goes. The second thing that might be out of order is using Update instead of FixedUpdate. The third is that the rigid body is doing its own thing once there is a collision. Prior to the collision, it is not doing anything. Maybe try something like the code you have, but use rigidbody.AddForce(vector3) in the FixedUpdate() to move it around. It is cool to see that you are really digging into the code and getting a solid understanding of Unity :)

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
-1

Answer by SarperS · Jun 27, 2012 at 07:01 AM

You can either switch your rigidbody to Kinematic, so it doesn't get affected by forces or use Rigidbody class's MovePosition and MoveRotation methods in FixedUpdate.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Car Hit Wall And Continue 2 Answers

Disable car bounce Wall (Car Hit Wall) 0 Answers

rigidbody Hitting wall question ? 0 Answers

View entire object when on intersection of wall 3 Answers

i can see throught the walls!? -1 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