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 question was closed May 31, 2014 at 08:26 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by martin-rohwedder · May 31, 2014 at 01:45 PM · collisionvector3move an object

Check which side of a cube my player collided with

I have this simple setup, where I have a moving player, and a cube object. When my player collides with the cube, he can press the key 'p' to push the cube.

However so far I have only found out to move the cube in a specific direction, by setting its vector. But what I want is to push the cube in the opposite direction of which side the player collided with.

I have tried to check out this answer - http://answers.unity3d.com/questions/339532/how-can-i-detect-which-side-of-a-box-i-collided-wi.html However I can't make the solution with a raycast work.

So I hope you will help me understand and find a solution to push my block in the oposite direction of the player, according to which side he has collided with.

My script attached to the cube is this

 using UnityEngine;
 using System.Collections;
 
 public class BlockBehaviorScript : MonoBehaviour {
 
     private Vector3 newPosition;
 
     // Use this for initialization
     void Start () {
         newPosition = transform.position;
     }
     
     // Update is called once per frame
     void Update () {
         transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime);
     }
 
     public void changePosition(Vector3 to) {
         newPosition = to;
     }
 
     void OnCollisionStay(Collision col) {
         Debug.Log(col.gameObject.name);
         if (Input.GetKeyDown(KeyCode.P))
         {
             Debug.Log("'P' has been pressed");
             changePosition(new Vector3(transform.position.x, 0.0f, newPosition.z + 4f));
         }
     }
 }
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by PouletFrit · May 31, 2014 at 05:07 PM

If I understand your problem correctly you want to be able to push a cube in the left right, up, bottom direction; no diagonals allowed... right? If that's the case pretty simple just find the direction, keep the larger axis value, then normalize your result and apply a constant:

 public float pushDistance;
 
 void OnCollisionStay(Collision col) {
     Debug.Log(col.gameObject.name);
     if (Input.GetKeyDown(KeyCode.P)) 
     {
         Debug.Log("'P' has been pressed");
         // Find where the player is from the cube
         Vector3 dir = transform.position - col.gameObject.transform.position;
         // Do not consider the y direction
         dir.y = 0;
         // Are we going to push in the x or z axis?
         if (Mathf.Abs(dir.x) > Mathf.Abs(dir.z)) 
         {
             dir.z = 0;
         } 
         else
         {
             dir.x = 0;
         }
         // Normalize then apply a constant to be sure that the distance from which the player push won't affect the speed of the movement
         dir.Normalize();
         changePosition(transform.position + dir*pushDistance);
     }
 }

If you wish to be able to push diagonaly just remove:

 if (Mathf.Abs(dir.x) > Mathf.Abs(dir.z)) 
 {
     dir.z = 0;
 } 
 else
 {
     dir.x = 0;
 }
Comment
Add comment · Show 5 · 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 martin-rohwedder · May 31, 2014 at 06:19 PM 0
Share

Hi and thanks for the answer.

When I am using your if statement it still pushes diognally. If I want to do so it doesn't, but only pushes linear from the side I am standing af, how can I do that??

avatar image PouletFrit · May 31, 2014 at 07:57 PM 0
Share

I corrected my script it should work now...

 changePosition(transform.position + dir*pushDistance);
avatar image martin-rohwedder · May 31, 2014 at 08:07 PM 0
Share

Thank you, it seems to work now. However, Do you know How I can make the block move a certain distance everytime?

PushDistance is that a vector for that purpose, and How do I initialize that vector?

$$anonymous$$now I use a float ins$$anonymous$$d of the variable pushDistance.

avatar image PouletFrit · May 31, 2014 at 08:27 PM 1
Share

pushDistance is a float and it's deter$$anonymous$$ing the distance the cube will move everytime. Since it's a public global variable, you can see it from the inspector.

you can multiply Vector3 by constant value (int,float,etc...)

exemple:

 float pushDistance = 5.0f;
 Vector3 dir = new Vector3(1, 0, 0);
 dir = dir * pushDistance;
 
 // Dir is now equal to (5, 0, 0);
avatar image martin-rohwedder · May 31, 2014 at 09:01 PM 0
Share

Thank you for your help

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Why does this script check for raycast collision? Is it necessary? 1 Answer

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

Respawn when a character hits an object with a tag of "Kill" , but after 3 deaths the controller is destroyed 0 Answers

Casting a Ray in the direction of the movement 1 Answer

Wht isn't the sphere moving and rebounding properly? 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