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 Pritchy · Jun 21, 2012 at 05:52 PM · movementcollisions

Script controlled Object moving along a surface.

Hi, I have an object, controlled by the player via a custom script, and when a key is pressed, control is taken away from the player and it moves along in a straight line until a collision occurs, where the object stops and control is returned to the player. The problem I'm having is when the object in question moves along a surface that the object should stop when hit. The object is touching the surface and thus this counts as a collision and stops movement, which is not the desired effect: Only direct collisions should stop the object. Another problem is that, when the object has been stopped by an obstacle, when the player chooses the direction of the object, it warps inside the obstacle and glitches. I'm using a box collider, a rigidbody and a custom script, posted at the bottom of this post. Any help would be greatly appreciated, and thank you in advance.

Code:

 // Declaration of Variables
  var Right: boolean;
  var Left: boolean;     
  var Up: boolean;
  var Down: boolean;
  var MoveSpeed: float = 12;
  
 //Movement
     
     
     function Update ()
     
     {
     
     
     //Right
         if(Input.GetKey("right") && !Left && !Up && !Down){
         Right
         = true;
     }
     
     if (Right) {
            transform.Translate(Vector3(0,0,MoveSpeed) * Time.deltaTime);
     }
 
 
  //Left
         if(Input.GetKey("left") && !Right && !Up && !Down){
         Left
         = true;
     }
      
     if (Left) {
            transform.Translate(Vector3(0,0,-MoveSpeed) * Time.deltaTime);
     }
     
     
      //Up 
         if(Input.GetKey("up") && !Left && !Right && !Down){
         Up
         = true;
     }
     
     if (Up) {
            transform.Translate(Vector3(-MoveSpeed,0,0) * Time.deltaTime);
     }
     
     
  //Down
         if(Input.GetKey("down") && !Left && !Up && !Right){
         Down
         = true;
     }
      
     if (Down) {
            transform.Translate(Vector3(MoveSpeed,0,0) * Time.deltaTime);
     }
     }
    
 function OnCollisionEnter(theCollision : Collision){
 if(theCollision.gameObject.name == "Blocker"){
 
 Down
         = false;
 Up
         = false;
 Left
         = false;
 Right
         = false;
 }        }
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

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiloblargh · Jun 21, 2012 at 06:31 PM

Make the object's box collider a little smaller than the grid size , attach it to an empty child object- have it center itself on collision and when a motion key is pressed reposition it relative to the object so it's flush with the current "front" side of its bounding box depending on which way it's moving.

     if(Input.GetKey("down") && !Left && !Up && !Right)
        {
        Down = true;
        colliderTransform.localPosition = (0, 0, -0.1)    }    

Also round the x and z components of the object's transform to integer values on colllision with a blocker.

Comment
Add comment · Show 4 · 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 Pritchy · Jun 21, 2012 at 06:57 PM 0
Share

Thank you so much, this looks like exactly what I was looking for! How would I got about Attaching the collider to an empty child object? I've never heard of doing this. Thanks, $$anonymous$$.

avatar image Kiloblargh · Jun 21, 2012 at 08:03 PM 0
Share

Actually never $$anonymous$$d, you don't need to do that, all you have to do is: (at the start of your script)

var myCollider : BoxCollider;

and on pressing the down key:

 myCollider.center = Vector3(0,0,-0.1);

then move it back to Vector3.zero when you collide.

avatar image Pritchy · Jun 21, 2012 at 09:39 PM 0
Share

UnassignedReferenceException: The variable myCollider of 'Player $$anonymous$$ovement' has not been assigned. You probably need to assign the myCollider variable of the Player $$anonymous$$ovement script in the inspector. UnityEngine.BoxCollider.set_center (Vector3 value) (at C:/BuildAgent/work/b0bcff80449a48aa/Runtime/ExportGenerated/Editor/NewDynamics.cs:1566) Player $$anonymous$$ovement.Update () (at Assets/Player $$anonymous$$ovement.js:54)

I get this error when I try to run the game. I've literally just copied what you said and pasted it in, and there is my error. Could you perhaps explain what the code here does? Thank you both for everything, I can tell the answer lies somewhere close!

avatar image Kiloblargh · Jun 22, 2012 at 06:51 AM 0
Share

"I've literally just copied what you said and pasted it in, and there is my error." http://unity3d.com/support/documentation/$$anonymous$$anual/Learning%20the%20Interface.html

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

My character is ignoring some collisions 3 Answers

Calculating collision behavior while using lerp 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I fix collisions while using this PlayerController script? 1 Answer

Collisions when pushing object 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