Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Benjamin6817 · Oct 07, 2012 at 08:18 AM · cameracollisionphysics

Camera Collision Help Thanks In Advance

Hello, all!

I have been making a developing a game with my friends, and we are pretty early into the development stage. I am one of the 3 programmers and we have been working on gameplay lately. But I recently noticed that the camera is going through walls and we need it fixed, but I don't know what to do.

But here's what I have tried:

I attached a collider component (didn't work) I attached a rigidbody component (didn't work) I attached made a new GameObject with a collider and rigidbody and had the camera be a child of it and vica versa I also made a C# script to make the camera follow the script:

using UnityEngine; using System.Collections.Generic;

public class FollowCamera : MonoBehaviour { public Transform CameraObject; using UnityEngine; using System.Collections.Generic;

 public class FollowCamera : MonoBehaviour
 {
 
     public Transform CameraObject;
 
     void Update()
     {
         // Move this transform towards the camera object.
         transform.Set(CameraObject.position.z, CameraObject.position.y,
                       CameraObject.position.z);
     }
 
 }

}

The mouse orbit script I used looks like this:

var target : Transform; var distance = 10.0; var height = 5.0;

var xSpeed = 250.0; var ySpeed = 120.0;

var yMinLimit = -20; var yMaxLimit = 80;

private var x = 0.0; private var y = 0.0;

var heightDamping = 2.0; var rotationDamping = 3.0;

function Start () { var angles = transform.eulerAngles; x = angles.y; y = angles.x;

// Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; }

function LateUpdate () {

if (Input.GetMouseButton(1)) { if (target) { x += Input.GetAxis("Mouse X") xSpeed 0.02; y -= Input.GetAxis("Mouse Y") ySpeed 0.02; y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0); var position = rotation Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation; transform.position = position; var wantedRotationAngle = target.eulerAngles.y; var wantedHeight = target.position.y + height; var currentRotationAngle = transform.eulerAngles.y; var currentHeight = transform.position.y; currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping
Time.deltaTime); currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping Time.deltaTime); var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0); transform.position = target.position; transform.position -= currentRotation Vector3.forward * distance; transform.position.y = currentHeight; } } }

static function ClampAngle (angle : float, min : float, max : float) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); }

It looks like this: alt text

But still nothing works. :-(

Any suggestions? And thanks in advance!

Regards, Ben

[EDIT]:

I have realized since the first post it was kind of vague so I included a screen shot of the game and attached the mouse orbit script I was using.

capture1.png (188.6 kB)
Comment
Add comment · Show 4
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
avatar image shaderop · Oct 07, 2012 at 09:38 AM 0
Share

When you say that the camera is going through walls, do you mean you're able to walk completely through them as if they are not there, or do you mean that the camera clips the wall geometry when it gets too close to it? As is the camera renders the wall as if part of it is sliced off near the camera?

avatar image Benjamin6817 · Oct 07, 2012 at 06:17 PM 0
Share

Yeah I have realized it was kind of vague, what's happening is the camera has the typical mouse orbit script that would come with Unity (only ours only orbits when you press the right mouse button) applied to it and the camera goes straight through the walls it looks really weird.

avatar image Benjamin6817 · Oct 07, 2012 at 06:28 PM 0
Share

Its a 3rd person multiplayer game, although we haven't finished the multiplayer aspect yet and its based off of LEGOs it takes place in the the LEGO people's destroyed world and they are trying to rebuild it.

avatar image Benjamin6817 · Oct 07, 2012 at 11:20 PM 0
Share

Seems pretty simple but I found an easy way to do it using Physics.Linecast, but thanks anyways!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Oct 07, 2012 at 09:48 AM

You can try ray casting (or probably spherecasting) from the cameras location to the thing it is focussed on. If the raycast doesn't hit the focussed object first then leave the camera where it was in the last frame (both position and rotation). I normally allow a number of different potential camera angles and if one remains unavailable for a few frames I switch to another valid one.

Other techniques include approaching the focussed object by reducing the distance from the camera if it is currently obscured or changing the angle of the camera towards an overhead view

Comment
Add comment · Show 1 · Share
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
avatar image Benjamin6817 · Oct 07, 2012 at 11:21 PM 0
Share

I used Physics.Linecast and it worked :D Thanks for the help.

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.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

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

Addforce to ball in camera position 0 Answers

How to use linecast ?? 1 Answer

AddForce to a point of a GameObject, not the center 1 Answer

Punching a crate 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