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 GunMetalGames · Jul 30, 2020 at 03:29 PM · raycastingmovement scriptplayer movementclippingtransform.translate

Translating Player Movement and Clipping Through Walls

Hello!

The Problem: I've been recently working on a pet project that involves the player moving in a grid-like manner. Using the normal forces method did not bring good results, and I landed on simply translating the player instead. For some reason, despite the fact that both the player and the environmental elements have Rigidbodies, they don't interact at all. My player just clips straight through the wall. I've provided a couple pictures to show this, as well as my code.
The Goal: allowing my player to move as-is, and be able to detect when they would collide with a wall. If they would collide with said wall, then they will move a set distance (let's say 0.5 units), then revert back to their original position. That way, there's visual feedback for the player to tell them that they can not collide with that wall.

Things I vaguely understand: I've seen other posts mention setting RB collision detection to "continuous dynamic", checking to make sure I have the proper colliders on both objects, editing the colliders to be wider, etc. These methods have not worked in my specific use case, and I predict that this is due to not using forces to move my player. I've seen one tutorial on Youtube mention raycasting, but that video's specific method did not work out.

Would greatly appreciate some answers/guides! Thank you! :)


Player hits wall one time from the initial starting position. Player is now able to freely clip through the wall.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody))]
 public class Movement : MonoBehaviour
 {
     public GameObject Player;
     Vector3 up = Vector3.zero;
     Vector3 nextPos, destination, direction;
     Vector3 currentDirection = Vector3.zero;
     public float Speed = 5f;
     bool canMove;
 
     private void Start()
     {
         currentDirection = up;
         nextPos = Vector3.forward;
         destination = transform.position;
     }
     // Update is called once per frame
     void Update()
 
     {
      
         // PLAYER MOVEMENT IS HERE
 
         if (Input.GetKeyDown(KeyCode.W))
         {
             transform.position += transform.forward; //MOVE FORWARD
         }
         else if (Input.GetKeyDown(KeyCode.S))
         {
             transform.position -= transform.forward; //MOVE BACKWARD
         }
         else if (Input.GetKeyDown(KeyCode.A))
         {
             transform.position -= transform.right; //STRAFE LEFT
         }
         else if (Input.GetKeyDown(KeyCode.D))
         {
             transform.position += transform.right; //STRAFE RIGHT
         }
 
         // THESE CONTROLS DETERMINE WHICH DIRECTION THE PLAYER IS FACING; CAMERA IS CURRENTLY A CHILD OF THE PLAYER, SO IT ALSO MOVES ACCORDING TO THESE CONTROLS
         if (Input.GetKeyDown("q"))
         {
             Player.transform.rotation *= Quaternion.Euler(0, -90, 0); //TURN CAMERA/PLAYER LEFT
         }
         if (Input.GetKeyDown("e"))
         {
             Player.transform.rotation *= Quaternion.Euler(0, 90, 0); //
         }
     }
 
 }

1.png (74.1 kB)
2.png (89.3 kB)
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
Best Answer

Answer by GunMetalGames · Aug 01, 2020 at 10:26 PM

So, for anyone who finds this in the future, here is what I ended up doing (for the time being). The person above, while not entirely helpful, at least gave me a decent place to start. If transform.translate doesn't interact with colliders, then you have to use a Rigidbody. I ended up working with Vector 3, because using rb.MovePosition didn't operate within local space (forward was always the same direction, no matter which way the player was facing). Here's my current code. Be aware that the longer it's been since this comment was made, the less likely it is that I'm still using it for my purposes.


 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {
 
     public GameObject player;
     public float speed = 0.5f; //this variable tells Unity how many units we want to move per input. In this case, we move half of 1 unit.
     public float rotationSpeed = 100.0f;
     private Rigidbody rb; //establishes a variable for this code, "rb" for "RigidBody"
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>(); //tells Unity that "rb" means we're using the RigidBody component
     }
 
    
     void Update()
     {
 
         // PLAYER MOVEMENT IS HERE
 
         Vector3 move = Vector3.zero;
 
         if (Input.GetKeyDown(KeyCode.W)) //move forward
         {
             move = transform.forward * speed;
         }
         else if (Input.GetKeyDown(KeyCode.S)) //move backward
         {
             move = -transform.forward * speed;
         }
 
         if (Input.GetKeyDown(KeyCode.A)) //move left
         {
             move = -transform.right * speed;
         }
         else if (Input.GetKeyDown(KeyCode.D)) //move right
         {
             move = transform.right * speed;
         }
 
         //rb.MovePosition(transform.position + move); (this comment is just me keeping a copy of the rb.MovePosition command, in case I need it later.)
         transform.position += move; //STRAFE RIGHT
 
 
 
         if (Input.GetKeyDown("q"))
         {
             transform.Rotate(0f, -90f, 0f);
         }
         if (Input.GetKeyDown("e"))
         {
             transform.Rotate(0f, 90f, 0f);
         }
 
     }
 
 }


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 itstimetomakelol · Jul 30, 2020 at 03:34 PM

transform.translate doesn't interact with colliders. Instead use rb.MovePosition or rb.AddForce.

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 GunMetalGames · Jul 30, 2020 at 09:10 PM 0
Share

So, I've tried using rb.$$anonymous$$ovePosition, and it still doesn't work. Collision is still not occurring between the wall and my player object.

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

142 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

Related Questions

How to make basic touch screen controls 1 Answer

Unity Player Smooth Left - Right Movement 1 Answer

How to move the other object without moving the first object? 1 Answer

Tiled Movement Inputs are Inaccurate 1 Answer

Similar movement to a game (look description) 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