Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Issues with camera shake upon collision with walls (corner of a room specifically)

Discussion in 'Scripting' started by bhoyle08, Feb 22, 2020.

Thread Status:
Not open for further replies.
  1. bhoyle08

    bhoyle08

    Joined:
    Feb 22, 2020
    Posts:
    11
    Hi all, so whenever my player walks to the corner of a room and collides with the two walls, the camera shakes violently, even when I walk away from the walls again, if I go back and walk into the corner long enough I can make the camera not shake anymore. By the way, I am using transform.translate movement, with rigidbody for collisions and gravity.
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    add tolerance
     
  3. bhoyle08

    bhoyle08

    Joined:
    Feb 22, 2020
    Posts:
    11
  4. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    post your code and we check
     
  5. bhoyle08

    bhoyle08

    Joined:
    Feb 22, 2020
    Posts:
    11
  6. bhoyle08

    bhoyle08

    Joined:
    Feb 22, 2020
    Posts:
    11
    CAMERA COLLISION SCRIPT
    public class CameraCollision : MonoBehaviour
    {

    public float minDistance = 0.8f;
    public float maxDistance = 2.2f;
    public float smooth = 5.0f;
    Vector3 dollyDir;
    public Vector3 dollyDirAdjusted;
    public float distance;

    void Awake()
    {
    dollyDir = transform.localPosition.normalized;
    distance = transform.localPosition.magnitude;

    }

    void Update()
    {
    Vector3 desiredCameraPos = transform.parent.TransformPoint(dollyDir * maxDistance);
    RaycastHit hit;

    if (Physics.Linecast (transform.parent.position, desiredCameraPos, out hit))
    {
    distance = Mathf.Clamp((hit.distance * 0.5f), minDistance, maxDistance);

    } else


    {
    distance = maxDistance;

    }



    transform.localPosition = Vector3.Lerp(transform.localPosition, dollyDir * distance, Time.deltaTime * smooth);





    }
    }


    PLAYER MOVEMENT SCRIPT
    public class PlayerController : MonoBehaviour {

    public float Speed;

    private void Update()
    {

    }

    void FixedUpdate() {
    PlayerMovement();

    }


    void PlayerMovement()
    {




    float hor = Input.GetAxis("Horizontal");
    float ver = Input.GetAxis("Vertical");
    Vector3 playerMovement = new Vector3(hor, 0, ver);
    playerMovement.Normalize();
    transform.Translate(playerMovement * Speed * Time.deltaTime, Space.Self);






    }


    }


    CAMERA CONTROLLER SCRIPT
    public class CameraController : MonoBehaviour
    {


    public float RotationSpeed = 1;
    public Transform Target, Player;
    float mouseX, mouseY;



    void Start()
    {

    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;

    }

    void LateUpdate()
    {
    CamControl();

    }


    void CamControl()

    {
    mouseX += Input.GetAxis("Mouse X") * RotationSpeed;
    mouseY -= Input.GetAxis("Mouse Y") * RotationSpeed;
    mouseY = Mathf.Clamp(mouseY, -55, 50);

    transform.LookAt(Target);


    Target.rotation = Quaternion.Euler(mouseY, mouseX, 0);
    Player.rotation = Quaternion.Euler(0, mouseX, 0);
    }






    }
     
  7. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    thanx will check later after dinner and movie
     
  8. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @bhoyle08

    Use code tags when posting code.

    "transform.translate movement, with rigidbody"

    You shouldn't most likely be using translate when you have a RigidBody, especially if it is not kinematic, it should be driven by Physics system. So use one of the RigidBody's move methods.

    Also, even if you use rb.MovePosition or such, you should check if you are / will be colliding with a wall when your character moves. If you move your character into another collider, this can cause jittering, when you force colliders into other colliders, and Physics system will separate those in next update. Get the input and calculate the direction and distance i.e. location where the next step would take your character to and see if there is an obstacle - if not, then allow movement.
     
  9. bhoyle08

    bhoyle08

    Joined:
    Feb 22, 2020
    Posts:
    11
    Thanks, will give it a go.
     
  10. bhoyle08

    bhoyle08

    Joined:
    Feb 22, 2020
    Posts:
    11
    @eses, To be honest, I'm really new to this, how would I begin to go about implementing rigidbody movement? And also, I understand what you mean about the collisions, but I wouldn't have a clue about how to implement that sort of system, is there any source I could reference to learn how to do that?
     
  11. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @bhoyle08

    New or not, could you please edit your post to use code tags...? I bet you get more help if you do so, as low effort posts don't give a good impression.

    "I wouldn't have a clue about how to implement that sort of system"

    There are several tutorials on this topic - just google or use Learning section.

    But all you have to basically do is what I said - Use RigidBody's move method from your objects attached rb, instead of using object's Transform to move it.
     
  12. bhoyle08

    bhoyle08

    Joined:
    Feb 22, 2020
    Posts:
    11
    OK so sorry to be giving you a low effort post? Maybe you should attempt to not be so pretentious (you're giving yourself a bad look). Something which comes across as easy, or obvious, doesn't apply to everybody else. Please if you're going to help people in the forums, get off your high horse. I wouldn't have a clue about how to implement that system, yeah? So what? Hold on a minute till I look up how to check for this, that, and the other? If I knew what to search, do you not think I would have done it already?
     
  13. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @bhoyle08

    "OK so sorry to be giving you a low effort post?"
    "Maybe you should attempt to not be so pretentious"

    "Please if you're going to help people in the forums, get off your high horse"

    OK, pretty strong assumptions and words - I guess from now on I won't point out that one should format posts, after all, it is the second pinned post after forum guidelines that explains and prompts people to format their code when posting. I just pointed it out, as you posted a wall of code, and clearly didn't see need to format it, even though it should be quite obvious, as you too can see it doesn't look like code in other posts - or does it? And I only said that "low effort" (= not formatted, no proper spelling, just some upper case "yelling") will not usually result in enthusiastic answers - guess it would have been better to ignore your post? It wasn't meant to be an "insult".

    And even though I now mentioned about using code tags - you didn't bother to edit your post?

    "If I knew what to search, do you not think I would have done it already?"

    I did already mention what to search, infact there is no need to search much, as I said: "Use RigidBody's move method from your objects attached rb, instead of using object's Transform to move it."

    You can find these things from manual, without guessing - RigidBody and Move(Position) - just type "unity rigidbody move" in google and you'll get the page. And like I said, there are several (official) tutorials on this topic, just open learning section and find any tutorial where you move some objects, most of those use rigid bodies.
     
  14. bhoyle08

    bhoyle08

    Joined:
    Feb 22, 2020
    Posts:
    11
    @eses Seems to me you're a very fussy person, yes my code may not be formatted, but it is there and easily understandable (the upper case spelling clearly points out what code represents what). Also "no proper spelling, just some upper case yelling", eses, you're not eminem. And yes I do believe you should have avoided my post, if you cant be amicable in the way that you approach unity forum "noobs" like myself, don't.
     
  15. bhoyle08

    bhoyle08

    Joined:
    Feb 22, 2020
    Posts:
    11
    @eses "Delete all was working now it isn't". You replied to this post telling him it would/ve been polite if he shared his solution (you were informing him in a non-direct way that he is rude), people come on these forums to solve their issues. You really need to chill out calling people things, it won't get you far.
     
  16. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    I see no pretentiousness in there reply. They only asked again for you to format the code correctly. The fact that you still haven't formatted the code makes you look bad.

    Asking you to use code tags is pretty straight forward, its called etiquette and is something Unity them selves ask us all to do. It's harder to read code in regular text format so that's why we have code tags. It does seem like very little effort is given when you don't format the code, so it is less likely to be answered or even looked at(I didn't bother cause its hard to read in its current format.)

    So if you could kindly edit that post and add the code tags, that would be great.
     
  17. alechalici

    alechalici

    Joined:
    Jun 28, 2019
    Posts:
    1
    @bhoyle08 My friend you have to stop assuming things and listen to the person that has 100x more posts than you. Show some respect from now on and don't tell someone how things are going to go when you are completely new to this and don't know what you are talking about. These two were very polite in trying to tell you what to do but I had to respond because you are being extremely rude to someone trying to help you. Don't take things so personally, just learn and move on. Good luck.
     
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    You're kicking a discussion/disagreement that happened in Feb 2020 and TBH is such a poor use for your first post. Let's leave this thread alone as there are far more productive things for you and others to be doing. :)
     
Thread Status:
Not open for further replies.