• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Integer value randomly multiples itself by 3 or 2 for seemingly no reason. 2 Answers

C#/Unity top-down 2Dgame newbie advice 0 Answers

Create 3d hexagonal terrain 1 Answer

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

Multiple Cars not working 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges