Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 BrownBoii333 · Apr 25, 2017 at 02:45 PM · c#3dbeginnertutorial

Make objects move around, then bounce off and continue moving. NOT WORKING

Hey I'm a first year College student and I'm pretty new to unity, I learned java in the past and am currently trying to learn c# and unity with tutorials and scripting tutorials. I completed the roll-a-ball tutorial but felt it might be a good idea to add something to it to as an extension to show i really learnt something. I decided to make the pick-up objects move around the world rather than staying still and then if they hit a wall (are close to a wall,and facing it) then turn 90 degrees (or a random angle) and keep going. But i've had a lot of trouble with this. I was able to make all the seperate pickup objects to move in different directions but they just go through the wall. I was able to make it so they stop if they get a certain distance close to the wall but the smaller i made that distance, the more that didn't actually stop, and even now not all stop. The code is messy and lots of stuff isn't used or has been changed dozens of times because i've tried ALOT of different solutions but idk how to do this. I thought this would be a fairly simple add on (just using dot product to see if its facing, then distance to see how close it is to the wall), or when it collides(tried that then got rid of it), but nothing has worked.

I'd appreciate some guidance as to how to do what i desire with this project.

Thanks (here is a sample of the script in which i tried to add this extra feature idk if it helps but i figured id add it)

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Moving : MonoBehaviour {
     private float xrandom;
     private float zrandom ;
     public Transform west;
     public Transform east;
     public Transform north;
     public Transform south;
 
     bool turn;
     Vector3 position;
     
 
     // Use this for initialization
     void Start () {
         xrandom = Random.Range(-10.0f, 10.0f);
         zrandom = Random.Range(-10.0f, 10.0f);
         west = GameObject.Find("WestWall").GetComponent<Transform>();
         east = GameObject.Find("EastWall").GetComponent<Transform>();
         north = GameObject.Find("NorthWall").GetComponent<Transform>();
         south = GameObject.Find("SouthWall").GetComponent<Transform>();
         // move(1);
     }
     
     // Update is called once per frame
     void Update () {
        
         if (!checkDistance() & !checkFacing())
         {
             move(1);
         }else
         {
             rotateOnce();
         }
     
     }
     bool checkDistance()
     {
         float wdist = Vector3.Distance(west.position, transform.position);
         float edist = Vector3.Distance(east.position, transform.position);
         float ndist = Vector3.Distance(north.position, transform.position);
         float sdist = Vector3.Distance(south.position, transform.position);
         if ((wdist <= 5)|| (edist <= 5)|| (ndist <= 5)|| (sdist <= 5))
         {
             //checkFacing();
             return true;
         }
         else
         {
             return false;
         }
     }
 
     bool checkFacing()
     {
         Vector3 wdir = (west.transform.position - transform.position).normalized;
         float wdirection = Vector3.Dot(wdir, transform.forward);
 
         Vector3 edir = (west.transform.position - transform.position).normalized;
         float edirection = Vector3.Dot(edir, transform.forward);
 
         Vector3 ndir = (west.transform.position - transform.position).normalized;
         float ndirection = Vector3.Dot(ndir, transform.forward);
 
         Vector3 sdir = (west.transform.position - transform.position).normalized;
         float sdirection = Vector3.Dot(sdir, transform.forward);
         if ((wdirection == 1)|| (edirection == 1)|| (ndirection == 1)|| (sdirection == 1))
         {
             return true;
         }else
         {
             return false;
         }
        
     }
     void rotateOnce()
     {
         turn = true;
         if (turn)
         {
             Vector3 rotation = new Vector3(0, 0, 90);
             transform.Rotate(rotation);
             turn = false;
         }
     }
     /*private void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Pickup"))
         {
             transform.Rotate(Vector3.up);
         }
         if(other.gameObject.CompareTag("Wall"))
         {
             transform.Rotate(Vector3.up);  
         }
     }*/
     void move(int neg)
     {
         position = new Vector3(xrandom*neg, 0.0f, zrandom*neg);
        // if (Input.GetKey("m")) {
             transform.Translate(position * Time.deltaTime, Space.World);
             //}
     }
 }
 
Comment
Add comment · Show 2
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 BaldBeardedMonk · Apr 27, 2017 at 07:13 PM 0
Share

You might want to look at Nav$$anonymous$$eshAgent. It is used for Ai/Enemy movements in the play area. Well this will be a totally new topic to you and you might need to spend some time to actually understand and implement it. but its worth as it very useful for any future projects as well.

avatar image BrownBoii333 BaldBeardedMonk · May 01, 2017 at 12:23 AM 0
Share

Theoretically speaking though, shouldn't using dot product for direction and distance work? Am I using it wrong or is that just not an option.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tMahon · May 01, 2017 at 09:57 PM

Have you considered using a Rigidbody to move the player and a physics material with a bounce value of 1?

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

8 People are following this question.

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

Related Questions

The unity tutorial roll-a-ball isn't working 1 Answer

Create 3d hexagonal terrain 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Brickshooter tutorial: bounding camera movement 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