• 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 /
  • Help Room /
avatar image
0
Question by Daville17 · Mar 05, 2017 at 10:17 PM · c#rigidbodyrigidbody2dmeshcollidercollison

Player is Going Through Walls

So I made a little 2d scene with the player and 4 walls but when ever I go to the wall i just go through it. I have Box Colliders, Mesh Colliders, and RigidBodys on all of my walls and Sphere Colliders, Mesh Colliders, and RigidBodys on my player as well. My Current Script is below . Im pretty sure that im not supposed to use "transform.position" because it doesnt look for collisons butIi dont know what to use then, and I'm not sure what setting the Box/Sphere Colliders, Mesh Colliders, and Rigid Bodys need to have.

Comment
Add comment · Show 1
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 Daville17 · Mar 05, 2017 at 10:25 PM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
  public class playermove : MonoBehaviour {
  
          float speed = 5.0f;
  
          void Update ()  {
                  var moveh = new Vector3(Input.GetAxis("Horizontal"), 0);
                  var movev = new Vector3(0 ,(Input.GetAxis("Vertical")));
                  transform.position += moveh * speed * Time.deltaTime;
                  transform.position += movev * speed * Time.deltaTime;
          }
  }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Commoble · Mar 05, 2017 at 11:15 PM

Set the player object's Rigidbody type to dynamic, make sure the mesh collider on the player is set to convex, and use Rigidbody.MovePosition(Vector3) to set your new position each Update instead of setting the transform.position manually.

Rigidbodies on moving objects have to be dynamic if you want other colliders to prevent the rigidbody from moving through them. T$$anonymous$$s is because the blocking is done by waiting for two colliders to overlap, then pus$$anonymous$$ng the colliders away from each other. Kinematic and static rigidbodies can't be pushed (i.e. be blocked by objects), they can only push other other objects away (they can only block other objects).

Comment
Add comment · Show 8 · 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 Daville17 · Mar 05, 2017 at 11:50 PM 0
Share

Thank you very much I was stuck on this all last night.

avatar image Daville17 · Mar 06, 2017 at 12:09 AM 0
Share

Wait, now im getting some errors. It's telling me that ("Unity.Engine.Vector3" is a Type but a Varible was expected) and then it says (Expression denotes a type where a varible value or method group was expected.)

avatar image Daville17 · Mar 06, 2017 at 12:18 AM 0
Share

Okay so I tried changing some things and i ended up with this But it is telling me Vector3 is already defined in this scope. Could you please show me what my script is supposed to look like?

avatar image Daville17 · Mar 06, 2017 at 12:24 AM 0
Share
     using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
       
       public class playermove : MonoBehaviour {
       
               float speed = 5.0f;
               privite Vector3;
       
               void Update ()  {
                       var moveh = new Vector3(Input.GetAxis("Horizontal"), 0);
                       var movev = new Vector3(0 ,(Input.GetAxis("Vertical")));
                       Rigidbody.MovePosition Vector3 = moveh * speed * Time.deltaTime;
                       Rigidbody.MovePosition Vector3 = movev * speed * Time.deltaTime;
               }
       }
avatar image Commoble Daville17 · Mar 06, 2017 at 01:18 AM 0
Share
 public Rigidbody body;    // link your rigidbody to the script in the inspector
 float speed = 0.5F;
 
 void Update()
 {
     Vector3 oldPos = this.transform.position;
     float newX = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
     float newY = Input.GetAxis("Vertical") * speed * Time.deltaTime;
     RigidBody.MovePosition(new Vector3(oldPos.x + newX, oldPos.y + newY, oldPos.z);
 }
 
 

I suggest reading the documentation on a function whenever you use one that you've never used before.

avatar image Daville17 Commoble · Mar 06, 2017 at 01:40 AM 0
Share

Thank you very much but how do you link your rigidbody to the script in the inspector?

avatar image Daville17 · Mar 06, 2017 at 02:39 AM 0
Share

How do you link your rigidbody to the script in the Inspector?

avatar image Commoble Daville17 · Mar 06, 2017 at 03:04 AM 0
Share

Drag the rigidbody component to the script.

I strongly suggest reading through unity's documentation on scripting, before you jump into it, especially controlling gameobjects and components with scripts.

avatar image
1

Answer by Willliam · Mar 06, 2017 at 01:15 AM

Try to add the Component ChracterController. and use ChracterController.Move(Vector3).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Object reference not set to an instance of an object 0 Answers

How to Change Rigidbody Type OnCollision/Trigger with Script? 1 Answer

Physics inconsistent even when using FixedUpdate() or Time.DeltaTime() 0 Answers

Move GameObject with transform.position 1 Answer

Rigidbody2D won't move player 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