My code is set to make the ball move to mouse position x, Instead, it move to the opposite side of mouse position x. Basically it runs from the mouse. Here is the code:
using System;
using UnityEngine;
public class BallTracker : MonoBehaviour
{
private float posX;
private float swidth;
public float rollSpeed = 5f;
public float sense = 3f;
public bool canTrack;
private bool win;
private void Awake()
{
this.swidth = (float)Screen.width;
if (Screen.width < Screen.height)
{
this.sense = 1f;
}
}
private void Update()
{
if (this.canTrack)
{
this.posX = Input.mousePosition.x + 1f;
this.posX /= this.swidth;
this.posX -= 0.5f;
this.posX = this.posX * 4f * 1.5f * this.sense;
base.transform.position = Vector3.Lerp(base.transform.position, new Vector3(this.posX, 0f, 0f), this.rollSpeed * Time.deltaTime);
}
if (this.win)
{
base.transform.position = Vector3.MoveTowards(base.transform.position, new Vector3(0f, 0f, 5f), 4f * Time.deltaTime);
}
}
public void Win()
{
this.canTrack = false;
this.win = true;
}
}
First of all this code is complete mess,for me at least.
First of all win function,why do you have “this.” if it is in the same script/class.It is unnecessary.
So what u gonna do is remove the “this” keyword:
…
public void Win()
{
canTrack = false;
win = true;
}
…
Its easier to manage and more more clear to look at.
And remove all the other “this” and “base” keywords they are unnecessary if the variables are from the same class/script.
…
And now the “running ball” part
Lets say your swidth(screen width if im not mistaking)
So swidth = 800;(example)
Since screen width isnt smaller than screen width the sensitivity(sense) = 3f;
posX = mousePosition.x + 1f;
So if i move the mouse right the posX will be 1f + 1f = 2f.
If i move it left its gonna be -1f+1f = 0.
Next thing you’re doing is dividing it by swidth
So its (in case you moved your mouse right) 1f+1f =2f.
2f/800 which is 0.0025f then youre substracting 0.5f from it
0.0025f - 0.5f = -0.4975f.
Then again spaghetti code:
…
this.posX = this.posX * 4f * 1.5f * this.sense;
…
So idk why you put 4f * 1.5f when you could have put *5.5f its the same really.
More accurate and simpler code would be:
…
posX *= 5.5f * sense;
…
Youre doing the same thing more efficiently multiplying posX by 4 and 1.5 and sense.
So that all equals:
…
-0.4975f *= 5.5f * 3f;
-2.73625f * 3f;
-8.20875f
So posX = -8.20875f;
Even tho you moved your mouse right all the calculations you made resulted in the posX being the opposite of what you wanted.
The result you wanted was 8.2 not -8.2
And now you moved your mouse right and got the opposite of -8.2f and when you lerp the ball is gonna roll left not right.I hope you got any of this i dont usually explain stuff very well.
now heres the code i think you shouldhave put
…
private float posX;
private float swidth;
public float rollSpeed = 5f;
public float sense = 3f;
public bool canTrack;
private bool win;
private void Awake()
{
swidth = (float)Screen.width;
if (Screen.width < Screen.height)
{
sense = 1f;
}
}
private void Update()
{
if (canTrack)
{
posX = Input.mousePosition.x;
posX *= sense;
transform.position = Vector3.Lerp(transform.position, new Vector3(posX, 0f, 0f), rollSpeed * Time.deltaTime);
}
if (win)
{
transform.position = Vector3.MoveTowards(transform.position, new Vector3(0f, 0f, 5f), 4f * Time.deltaTime);
}
}
public void Win()
{
canTrack = false;
win = true;
}
…
Thats it and please don’t type “this” and “base” anymore,you are just waisting time doing that.I hope my explanation helped a bit.