Ape
August 10, 2017, 9:30am
1
Hello,
i started creating my first game with unity. I try to build Frogger.
My Problem is, that when a car hits the Frog, he jumps to the spawn point only for the time, when the collider of the car is over the collider of the frog. When the car passed, the frog in on the “old” position.
Part of Frog Script:
public Vector3 respawnPoint;
void OnTriggerEnter2D (Collider2D col)
if (col.gameObject.tag == "Car")
{
Debug.Log("You Lost");
Life -= 1;
transform.position = respawnPoint;
}
Anybody an idea of fixing this problem?
mgear
August 10, 2017, 9:37am
2
Hi,
What code you have in the Update() loop?
maybe there is some position variable, that is not resetted on the collision…?
Ape
August 10, 2017, 10:14am
3
I am not sure if anybody can understand this but this is my Update()
void FixedUpdate() {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
animUsed = 0;
if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.parent = null;
float right = 0.5f;
newPosition = rb.position + Vector2.right * right;
newPosition.x = Mathf.Clamp(newPosition.x, -mapWidh, mapWidh);
transform.localRotation = Quaternion.Euler(0, 0, 90);
JumpMeth();
Asourcejump.Play();
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.parent = null;
float left = 0.5f;
newPosition = rb.position + Vector2.left * left;
newPosition.x = Mathf.Clamp(newPosition.x, -mapWidh, mapWidh);
transform.localRotation = Quaternion.Euler(0, 0, -90);
JumpMeth();
Asourcejump.Play();
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
transform.parent = null;
float y = (Input.GetAxis("Horizontal")) + Spur;
newPosition = rb.position + Vector2.up * y;
newPosition.y = Mathf.Clamp(newPosition.y, 0, mapHigh);
transform.localRotation = Quaternion.Euler(0, 0, 180);
JumpMeth();
Asourcejump.Play();
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
float y = (Input.GetAxis("Horizontal")) + Spur;
newPosition = rb.position + Vector2.down * y;
newPosition.y = Mathf.Clamp(newPosition.y, 0, mapHigh);
transform.localRotation = Quaternion.Euler(0, 0, 360);
JumpMeth();
Asourcejump.Play();
}
rb.MovePosition(newPosition);
if (Leben <= 0)
{
EndGame();
}
}
mgear
August 10, 2017, 10:20am
4
you are moving transform and in fixedupdate using rigidbody position,
so probably rb doesn’t get moved fast enough…
try moving rigidbody directly instead:
*also, can use code tags, so its easier to see script
If you see someone who needs to know about code tags, please link them to this post: Please use code tags when posting code. You can "tag" your code by typing around your code. There is no overt "Code" tag button that automatically tags a...
Reading time: 9 mins 🕑
Likes: 83 ❤
Ape
August 10, 2017, 10:42am
5
thanky you for your fast reply.
I tried using only moving by rigidbody and moving only by transform but both doesn’t work.
When I move the Frog in the time he is on the spawn point, he is not teleported back when the car passed.
Any other ideas?