- Home /
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);
//}
}
}
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.
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.
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?
Your answer
Follow this Question
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