0
votes

I want to move object to right and to left and i have a weird problem when doing it. I am doing Transform.Translate in update method with If(Input..)

This is my code for moving to right

player.transform.Translate(Vector3.right * Time.deltaTime * varLeftRight, Space.World);

(I tried to use ("player") with gameobject attached in inspector and I tried to use rigidbody ("b") but it doesn't help same issue happens)) where the variable varLeftRight is 125.

I am adding force to go forward in oncollision like this:

b.AddForce(Vector3.forward * speed);

And lets say my player is on coordinates:
-0.074 ->x
-1.5166 ->y
4.173041 -z

I put on player rigidbody is kinematic because I want to test why same issue happens when playing. The problem is when I click once it works and moves right

Cordinates with one click:
1.592691
-1.5166
4.173041

But the problem is when i click twice very fast the player position brakes and goes too much to right.

With twice fast clicks to go to right he goes too much and when I get back one field he need to be on this coordinates:
1.592691
-1.5166
4.173041

and he is on this cordinates:
2.101478
-1.5166
4.173041

Idk did I described issue good. I hope you will understand me, so when I click once to move with trasnform.translate everything works fine when I click twice quickly he goes too much to right and when I get back one field to see where is he with one click he is not on right place, to go back to left one field is the same method only with adding left parametar like this:

player.transform.Translate(Vector3.left * Time.deltaTime*varLeftRight, Space.World);

I tried removing the Space.World but it doesn't help.

btw: my obstacle collision have all rigidbody is kinematic to true (Tried without rigidbody same issue happens.

In on Collision I am adding force to go forward no other scripts in code.

public class PlayerControl : MonoBehaviour
{
    public float bounce;
    public float speed;
    public float right;
    Rigidbody rb;
    public GameObject player;
    public float varLeftRight;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.AddForce(Vector3.forward * speed);
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("add force");
            player.transform.Translate(Vector3.right * Time.deltaTime * varLeftRight, Space.World);
        }
        if (Input.GetMouseButtonDown(1))
        { 
            player.transform.Translate(Vector3.left * Time.deltaTime*varLeftRight, Space.World);
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        rb.AddForce(Vector3.forward * speed);
    }
}
1
please rather just paste the script with a short description of your problem, the wordy description is quite confusing. it probably is a lot easier to help if we can see the whole thing. - yes
@yes Thank you for your suggestion I edit the question and put the code inside. I hope someone will help me with solution. - NoviKorisnikk

1 Answers

0
votes

I couldn't make sense of why you're adding force on collision, but I have a feeling that's where the issue lies. I'd comment that out to see if you're still having an issue.

Say that's really the problem, maybe consider only adding force on collision on the proper circumstances (ie. if the player is just colliding with the ground should they get force? or just if colliding with certain elements? etc)