• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MudkipInDisguise · Jun 09, 2020 at 08:51 PM · cameracollisionphysicsrigidbodycollider

Camera gets flung off of the map when the player collides with certain objects.

When the player collides with a collider and I move forward a lot, the camera gets flung far from the map and orbits it when I look around. How do I fix t$$anonymous$$s?

Here is a video showing the issue

Here is the player controller Script (I deleted some parts that have not$$anonymous$$ng to do with movement because it is a huge script)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 using UnityEngine.UI;
 
 
 public class PlayerController : MonoBehaviour
 {
     public float MaxSpeed;
     public int damage = 1;
     public float attackCool;
     public int HP;
     public float vert;
     public float horz;
     public float speed;
     public float lookx;
     public float looky;
     public float minx;
     public float maxx;
     public float sens;
     private float x;
     private float y;
     private float z;
     public int forceConst = 50;
     private Rigidbody body;
     public GameObject cam;
     public bool onGround;
     public Vector3 crosshare;
     public GameObject[] place;
     public int select;
     private LineRenderer line;
     private Text HPdisp;
     public int range;
     public GameObject maxTarget;
     public bool fps;
     public Camera t$$anonymous$$rdPerson;
     private Camera firstPerson;
     public GameObject preview;
     private bool canPlace;
     private bool canBreak;
     public bool canAttack;
     private RaycastHit delete;
     private RaycastHit create;
     public RaycastHit attack;
     public float minDist;
     public GameObject Weapon;
     private Quaternion weaponOgAngles;
     private GameObject overworld;
     private GameObject underworld;
     // Start is called before the first frame update
     void Start()
     {
     
         body = GetComponent<Rigidbody>();
         line = cam.GetComponent<LineRenderer>();

     }
 
     // Update is called once per frame
     void Update()
     {
         
         
             body.velocity = Vector3.ClampMagnitude(body.velocity, MaxSpeed);
         
     
     maxTarget.transform.localPosition = new Vector3(0, 0, range);
         Cursor.lockState = CursorLockMode.Locked;
         //Movement Keys
         vert = Input.GetAxis("Vertical");
         horz = Input.GetAxis("Horizontal");
         x = horz * speed;
         z = vert * speed;
         //
         
               body.AddRelativeForce(x, 0f, z);
         //
         if (Input.GetKeyDown(KeyCode.P) && fps)
         {
             t$$anonymous$$rdPerson.enabled = true;
             firstPerson.enabled = false;
         }
         if (Input.GetKeyDown(KeyCode.P) & !fps)
         {
             t$$anonymous$$rdPerson.enabled = false;
             firstPerson.enabled = true;
         }
         //Look around
         lookx += Input.GetAxis("Mouse Y");
         lookx = Mathf.Clamp(lookx, minx, maxx);
         looky += Input.GetAxis("Mouse X");
         cam.transform.eulerAngles = new Vector3(-lookx * sens, looky * sens, 0);
         transform.eulerAngles = new Vector3(0, looky * sens, 0);
         //Jump
         if (Input.GetKeyDown(KeyCode.Space) && onGround == true)
         {
             body.AddForce(0, forceConst, 0, ForceMode.Impulse);
             onGround = false;
         }
         
         //Place Blocks
         
         crosshare = cam.GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 0, 0));
         
         preview.transform.position = create.point;
         
         canPlace = Physics.Raycast(crosshare, cam.transform.forward, out create, range);
         canBreak = Physics.Raycast(crosshare, cam.transform.forward, out delete, range);
         canAttack = Physics.Raycast(crosshare, cam.transform.forward, out attack, range);
 
         if (create.distance == 0)
         {
             preview.transform.position = maxTarget.transform.position;
         }
         if (create.distance < minDist)
         {
             canPlace = false;
         }
         if (Input.GetMouseButtonDown(1))
         {
             preview.SetActive(true);
             if (canPlace == true)
             {
                 Instantiate(place[select], create.point, cam.transform.rotation);
             }
             if (create.distance == 0)
             {
                 Instantiate(place[select], maxTarget.transform.position, cam.transform.rotation);
             }
            Debug.Log(create.distance);
         }
 
         //Break them
         if (Input.GetMouseButtonDown(0))
         {
             preview.SetActive(true);
             crosshare = cam.GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 0, 0));
             if (canBreak == true)
             {
                 if (delete.collider.tag == "Blocks")
                 {
                     Destroy(delete.collider);
                 }
             }
         }
         
         
     private void OnCollisionEnter(Collision collider)
     {
         if (collider.collider.tag == "Walkable" || collider.collider.tag == "Blocks")
         {
             onGround = true;
         }
     }
     private void OnCollisionStay(Collision collider)
     {
         if (collider.collider.tag == "Entity" && 
 
 
 
 }
 
 
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

313 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get collisions on Character controller? 1 Answer

How to make two colliders or rigidbodies stick together upon collision, as if there was a glue on their surfaces? 3 Answers

Advice for Intersecting Colliders 0 Answers

OnCollisionEnter isnt called when player lands on object 1 Answer

Limiting rigidbody physics influence on another rigidbody 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges