Hello everyone. I am new to unity and C#,
I can’t figure out a way to prevent the player object from stopping vibrating when I keep pressing against a static collider:
dufd7m
the script of the player movement is shown in the video
I will also send an image of the rigid bodies of both the player and the “arcade”
I was also thinking maybe to get rid of the rigidbodys and only use collider… and somehow just come up with a function that says that on specific x and y range the player cant moves further…
if i could get rid of rigidbodys I think it would be the best for me
Since you posted zero code, I’ll just take a wild guess:
With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.
Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.
This is the script for the player movement.
It seems to me that rigidbodys aren’t necessary if I can’t just come up with an idea to set the boundaries in another way.
I will read the links you provided tomorrow morning since I g2g to bed lol. thank you for responding and providing more information.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
public Sprite rightSprite;
public Sprite leftSprite;
public Sprite staticSprite;
void Start()
{
}
void Update()
{
if(Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(new Vector3(5 * Time.deltaTime,0,0));
this.gameObject.GetComponent<SpriteRenderer>().sprite = rightSprite;
}
else if(Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(new Vector3(-5 * Time.deltaTime, 0, 0));
this.gameObject.GetComponent<SpriteRenderer>().sprite = leftSprite;
}
else if(!Input.anyKey)
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = staticSprite;
}
else if(Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(new Vector3(0, 5 * Time.deltaTime, 0));
this.gameObject.GetComponent<SpriteRenderer>().sprite = staticSprite;
}
else if(Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(new Vector3(0, -5 * Time.deltaTime, 0));
this.gameObject.GetComponent<SpriteRenderer>().sprite = staticSprite;
}
}
}
Follow what Kurt has said carefully. By directly changing the Transform, you cause the Rigidbody2D to have to read and adopt that new position when the next simulation step begins. The simulation then has to solve the overlap(s) you are causing (by you modifying the Transform) and this repeats again and again because you’re changing the Transform.
One of the things the physics system does is try to stop overlaps in the first place, especially when using Continuous Collision Detection.
Rule#1: Use the Rigidbody2D API to cause movement. If you’re modifying the Transform when using 2D physics components, you’re doing it wrong.