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 Phil 9 · Jan 16, 2011 at 05:29 PM · cameracollisionraycastcollide

Camera raycast?

hey all! i was doing peteys from burgzergarcades tutorial series camera part and when he made his camera he didnt add collision. i was wondering if you guys wanted to help me figure this out i want the camera to collide with everything in the game and i heard some people using raycast. i am noobie at scripting and never done raycasting but this is it and sorry if it gets cut off half way through dont know hot to fix that :

using UnityEngine; using System.Collections;

public class RPGCamera : MonoBehaviour { public Transform target; public float walkDistance; public float runDistance; public float height; public float xSpeed = 250.0f; public float ySpeed = 120.0f; public float heightDamping = 2.0f; public float rotationDamping = 3.0f;

private Transform _myTransform; private float x; private float y; private bool _camButtonDown = false;

void Awake() { _myTransform = transform;

} // Use this for initialization void Start () { if(target == null) { Debug.LogWarning("We do not have a target for the camera"); } else { CameraSetup(); } }

void Update() { if(Input.GetMouseButtonDown(1)) _camButtonDown = true; if(Input.GetMouseButtonUp(1)) _camButtonDown = false; }

void LateUpdate() { if(target != null) {

     if(_camButtonDown) {        

         x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

// y = ClampAngle(y, yMinLimit, yMaxLimit);

         Quaternion rotation = Quaternion.Euler(y, x, 0);
         Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position;

         _myTransform.rotation = rotation;
         _myTransform.position = position;

     }
     else {

// _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance); // _myTransform.LookAt(target); x = 0; y = 0;

// Calculate the current rotation angles float wantedRotationAngle = target.eulerAngles.y; float wantedHeight = target.position.y + height;

         float currentRotationAngle = _myTransform.eulerAngles.y;
         float currentHeight = _myTransform.position.y;

         // Damp the rotation around the y-axis
         currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

         // Damp the height
         currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

         // Convert the angle into a rotation
         Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);

         // Set the position of the camera on the x-z plane to:
         // distance meters behind the target
         _myTransform.position = target.position;
         _myTransform.position -= currentRotation * Vector3.forward * walkDistance;

         // Set the height of the camera
         _myTransform.position = new Vector3 (_myTransform.position.x, currentHeight, _myTransform.position.z);

         // Always look at the target
         _myTransform.LookAt (target);

     }
 }

}

public void CameraSetup() { _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance); _myTransform.LookAt(target);

}

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by The_r0nin · Jan 16, 2011 at 07:19 PM

If you want the camera to collide, simply add a collider and a rigidbody. That way it will collide with the other objects in the scene. Unless you wanted some other specific behavior that isn't clear from your question...

Comment
Add comment · Show 5 · 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 Phil 9 · Jan 20, 2011 at 02:16 AM 0
Share

That doesnt work with a camera. and rigidbody makes it fall, with the behavior i want, i want raycast do that if you hit it, it will bump forward alittle bit and if it collides with like a house or something it will go straight to the back of the player, adding a rigidbody just makes it fall to the ground...

avatar image The_r0nin · Jan 20, 2011 at 04:54 PM 0
Share

Click is$$anonymous$$inematic under the rigidbody, and it will only move when it is scripted to move, but it will still collide and send OnCollision info, so you can script your behavior from there...

avatar image Phil 9 · Jan 23, 2011 at 02:14 AM 0
Share

I mean even if i do that it will still go through because i am forcing it through and it not do anything because if you click and drag it will still go through.

avatar image The_r0nin · Jan 23, 2011 at 07:48 PM 0
Share

You have to script the reactions you want. In the OnCollision function, you script how it will behave. That's what you would have to do with a raycast, but raycast will eat up more processing. If there is any other way to do what you want without scripting... I don't know it...

avatar image Phil 9 · Jan 30, 2011 at 01:39 AM 0
Share

Lol, i just dont think that it will work because even if i do try that it still dont work,but thanks for the help man.

avatar image
0
Wiki

Answer by JJNCreator · Aug 13, 2012 at 04:35 PM

Easy. Make a private float called distanceToGround. Put this code in the Update function:

 if (Physics.Raycast(_myTransform.position, -Vector3.up, out hit, 100.0f)) {

distanceToGround = hit.distance;

Then, times the walk distance in the Vector3 position variable by distanceToGround. Then you shouldn't have any errors. You might want to edit my code as I spent all day working on it and it isn't perfect, but it's good enough for now.

(if post doesn't come out right, please edit).

Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

3rd Person camera collision 2 Answers

Camera collision detection. 0 Answers

Camera collision with raycasting 1 Answer

Ray Cast not working for camera. 1 Answer

Can someone modify this for me? 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