This is a very basic issue, so I apologise to anyone who gets offended by my post.
I am having confusion about moving objects using scripting. I have spent much time on the docs, examples, but remain confused.
I have a scene wherein I have a collection of objects at a starting position. These objects then move away from the centre of the scene using
rb.velocity = Random.insideUnitCircle * forceMult;
This works great. I detect when an object goes off screen by comparing its position with two known values, bounceX and bounceY, being the X and Y edges of the area visible through the camera.
float posX = Mathf.Abs(transform.position.x);
float posY = Mathf.Abs(transform.position.y);
//float posZ = Mathf.Abs(transform.position.z);
if (posX > bounceX)
{
//do something
}
if (posY > bounceY)
{
// do something
}
When either of these cases occur, I want to send the object back into the scene towards its starting position, which I have stored as a Vector3 in the start() routine.
No matter what I have tried, I can not get it to work. I have tried a similar fashion to starting the object’s movement specifying the starting vector as the end position but this seems to send the object in any direction. I have tried using transform.position but also can not get this to work. I have tried Vector3.Lerp but can not get this to work either.
The issue is that I basically do not understand Vector3 nor insideUnitCircle and an object’s velocity. I think what I need is a way to move an object to a known end position in a movement-type manner not a snap behaviour…
Can someone point me to a simple explanation of these things with some practical examples.
My object’s script is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class createMovement : MonoBehaviour
{
public float forceMult = 5;
private Rigidbody2D rb;
public float bounceX = 60f;
public float bounceY = 40f;
private string ownerArtwork;
private Vector3 homeVector;
private int numberOfBounces = 3;
public int stopMovement = 0;
public float constantSpeed = 10f;
private bool resetDirection = false;
private void Awake()
{
Debug.Log(Random.insideUnitCircle);
}
private void Start()
{
rb = GetComponent<Rigidbody2D>();
homeVector = new Vector3(rb.position.x, rb.position.y, 0);
rb.bodyType = RigidbodyType2D.Dynamic;
int index = transform.name.IndexOf("_") + 1;
ownerArtwork = transform.name.Substring(0,index) + "00";
objectManager.Instance.AddShapeObject(transform.name, homeVector);
rb.velocity = Random.insideUnitCircle * forceMult;
rb.velocity = constantSpeed * (rb.velocity.normalized);
}
// Update is called once per frame
void FixedUpdate()
{
float posX = Mathf.Abs(transform.position.x);
float posY = Mathf.Abs(transform.position.y);
//float posZ = Mathf.Abs(transform.position.z);
if (posX < bounceX && posY < bounceY) {
//object inside scene
if (resetDirection) {
//inside scene and direction has been reset so clear reset flag
resetDirection = false;
}
}
else if (posX > bounceX)
{
if (resetDirection)
{
// direction has been reset so do nothing
}
else {
//transform.position = Vector3.Lerp(transform.position, homeVector, Time.deltaTime);
rb.velocity = homeVector;
rb.velocity = constantSpeed * (rb.velocity.normalized);
resetDirection = true;
}
}
else if (posY > bounceY) {
if (resetDirection)
{
//direction has been reset so do nothing
}
else
{
//transform.position = Vector3.Lerp(transform.position, homeVector, Time.deltaTime);
rb.velocity = homeVector;
rb.velocity = constantSpeed * (rb.velocity.normalized);
resetDirection = true;
}
}
}
void positionChanging() {
transform.position = Vector3.Lerp(transform.position, homeVector, Time.deltaTime);
}
}