Sure, at least you can see how it’s working, right?
I think it’s a step in the right direction…
Oh boy, I am unable to implement different solutions.
I will post full code so I hope you guys will understand better what I want to achieve.
This code is attached to a moving ball.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class gulaSila : MonoBehaviour{
public float rychlost = 4.0f;
private float x, z, tolerancia;
private Rigidbody body;
private Vector3 v, spawn, kurzor;
private Scene scena;
private string nazovSceny;
public bool moze = true;
void Start(){
body = GetComponent<Rigidbody>();
scena = SceneManager.GetActiveScene();
nazovSceny = scena.name;
spawn = new Vector3(0, 1.5f, 6.0f);
tolerancia = 0.01f;
}
void Update(){
RaycastHit hit;
kurzor = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(kurzor);
if(Input.GetMouseButtonDown(0)){
if(Physics.Raycast(ray, out hit)){
if(hit.collider.tag == "sipky"){
hit.transform.Rotate(0.0f, 90.0f, 0.0f);
//Debug.Log(hit.transform.rotation.eulerAngles.y);
}
}
}
}
void FixedUpdate(){
v = new Vector3(x, 0.0f, z);
v.y = body.velocity.y;
body.velocity = v;
Debug.Log(transform.position.x + " | " + /*transform.position.y +*/ " | " + transform.position.z);
if(Input.GetKeyDown(KeyCode.Space) && moze){
z = -rychlost;
x = 0.0f;
moze = false;
}
RaycastHit hit_2;
Ray ray_2 = new Ray(transform.position, Vector3.down);
float hitX, hitZ;
Vector3 vHit;
if(Physics.Raycast(ray_2, out hit_2)){
//Debug.DrawRay(ray_2.origin, ray_2.direction, Color.green);
//Debug.Log(hit_2.distance);
//Debug.Log(transform.position.y);
if(hit_2.collider.tag == "sipky"){
hitX = hit_2.transform.position.x;
hitZ = hit_2.transform.position.z;
vHit = new Vector3(hitX, transform.position.y, hitZ);
if((Mathf.Abs(transform.position.x - hitX) <= tolerancia) &&
(Mathf.Abs(transform.position.z - hitZ) <= tolerancia)){
/*transform.position = vHit; -- this was supposed to be one of the solutions but instead of doing this once,
it's doing it whole time every frame which makes the object look like it's stuck in that position*/
if(hit_2.transform.rotation.eulerAngles.y == 0){
x = 0.0f;
z = -rychlost;
}
else if(hit_2.transform.rotation.eulerAngles.y == 90){
x = -rychlost;
z = 0.0f;
}
else if(hit_2.transform.rotation.eulerAngles.y == 180){
x = 0.0f;
z = rychlost;
}
else if(hit_2.transform.rotation.eulerAngles.y == 270){
x = rychlost;
z = 0.0f;
}
}
}
}
}
void OnCollisionEnter(Collision c){
if(c.gameObject.tag == "znicit"){
transform.position = spawn;
transform.rotation = Quaternion.identity;
body.angularVelocity = Vector3.zero;
z = 0.0f;
x = 0.0f;
moze = true;
}
/*else if(c.gameObject.tag == "opacnySmer"){ // ignore this...
if(r == (r * -1)){
r = (r * -1);
}
else{
r = (r * -1);
}
}*/
/*else if(c.gameObject.tag == "doLevelu_2"){
}*/
}
}
Could you provide an update about what’s happening with the ball? Is anything happening the way you want it to?
Lets say that there are 2 platforms beside each other with the same functionality to redirect the ball depending how is platform rotated by clicking on it and yes the platform rotates instantly.
Now I press spacebar to move the ball at the constant speed by changing its velocity directly and in the initial desired direction “-rychlost” which is right direction.
Now it continues to roll perfectly and it hits the first platform that redirects the ball in a direction the platform itself is positioned in. It hits approx range between current position and the center of platform with acceptable range being 0.01 so its not exactly center but it does a job and redirects the ball however because it was not exactly center it continues to the next platform with not very good axis value being lets say 0.99754 or so and the next time it hits the second platform it ignores redirecting and continues to roll in the direction it was directed the first time.
I wanted to solve this problem by setting the balls position immediately to the center of the platform when it hits desired range which is small so that it will be at perfect integer axis value and the player wouldnt even spot the instant position change because of how small the position difference would be.
But I dont know how to set the balls position to desired one being center of the platform only once - the code portion that is commented is not setting balls position just once but continually every frame and it makes the ball looks stuck in place.
I want this to be simple ping pong looking game where the ball is redirected from one center to another with perfect integer values and such.
If this aint enough then I am quite without idea as to how I should I explain it
the ball goes back & forth between 2 platforms… I’ve got that so far. Do the platforms move? I’d like to understand what went wrong between my previous suggestions & now (or what I’ve misunderstood that I could try to correct to offer you a better suggestion). I totally get the part about you want it to go back to the centre, etc…
You could include a picture if you think that might help.
Oh man … I will try to make short video about it then…
when I get home
lol sorry. That would be helpful, I think. I try to follow what you’re talking about. Sometimes I think I have got it, but other times I am not sure. I will keep watching this thread when I can.
Hmm it will be a while before I get home.
Would you please try the code and recreate scene? Sorry if I ask too much and maybe impossible … it’s just that this problem persists for weeks and I am always thinking about possible solutions … I literally cant sleep because I just have to think about it ![]()
You only need rigidbody ball with attached script to it, some plane to walk upon and platform with a tag that redirects the ball. Try to set 2 platforms beside each other and direct them so that they will ping pong the ball between each other when ball walk upon the center of that platform.
If you have no idea what I mean then its alright but man am I desperate ![]()
I can try to recreate it next time I’m around, for sure. Gotta get some sleep soon. But if you showed a screen shot (or short video) then I could create it (or try to) more accurately.
So anyone? I can’t really do more to explain my problem.
Okay, I wasn’t around until now… Thank you for the video. it is 4 places! that helps a lot at understanding. For a long time, I kept thinking it’s just 2 places. What I would in your situation is make an array for locations that each ‘Arrow Square’ can possibly point to, to give the locations it could go based on the arrow.
When the arrow changes , update the index of the array (ie: point to the relevant spot).
Have a small trigger (mini cube/sphere) at the arrow, and when it gets there, stop its velocity and set it’s new source towards the location (… same as the index in the array).
K, I made a little project. It doesn’t look as cool as yours but it’s working. It could use some improvements, but it’s not a bad place to start.
Here’s what I did:
This script was just ‘setup’ for me to put the sphere in the right spot lol. I’m bad with dragging things. So you might not even care about this one.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spheremove : MonoBehaviour {
[SerializeField]
Rigidbody Sphere;
[SerializeField]
Transform StartPos;
private void Start()
{
Sphere.transform.position = StartPos.position;
}
}
Next up was this…:This goes on the Sphere, of course. You can get rid of the ‘begin’, which i just used to test it out. You can adjust the speed.
public class sphereroll : MonoBehaviour {
public Transform destination;
bool begin = false;
Rigidbody mRigidbody;
float movespeed = 5f;
private void Awake()
{
mRigidbody = GetComponent<Rigidbody>();
}
private void Update()
{
if (Input.GetMouseButtonUp(0)) { begin = true; }
}
private void FixedUpdate()
{
if (!begin) return;
mRigidbody.velocity = (destination.position - transform.position).normalized * movespeed;
}
}
}
And finally this script when on each of the 4 spots i had setup:
public class triggerforsphere : MonoBehaviour {
[SerializeField]
Transform destinationNext;
private void OnTriggerEnter(Collider other)
{
other.GetComponent<sphereroll>().destination = destinationNext;
}
}
I manually setup the ‘destinationNext’ variable and didn’t use an array that changed based on clicking the arrow, for the demo…
If you try my small bits of code, I think you can see how it could be modified to suit your needs for the arrows.
Are you still working on this? Did you try my code bits?
I had a day break so sorry for no feedback. I can’t seem to understand completely this code bits but I will try it tho.
By having destination as public and so, I need to explicitly specify destination? I want it to automatically store the destination(center of platform) whenever it rolls over it and then bounce in different direction from that exact center having no weird float values in X, Y or Z.
Are the collisions necessary?
Because I planned to do this purely by Raycasts.
I used cubes as the public destinationNext transform variables. i made them small and gave them a trigger. In your game, I’d imagine you would just disable the renderer (so they’d be invisible to the user). The cubes, I place them in the centre of the platform area.
The part I didn’t do was add a list/array of possible “center of platforms” that you can get to , when you change the arrow. That would be simple to add, after you try & see what I did/wrote.
So, the 4 mini cubes in the center of each platform. Imagine I started on the bottom left, I dragged the bottom right cube into the bottom left’s "destinationNext’ variable. Then I did the same with upper right dragging to lower left. And again, upper left dragging to upper right. You get the idea? ![]()
Try my idea. Do not sweat raycast vs the trigger for this moment, just get a look at it ![]()