• 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 Ravart · Mar 09, 2011 at 02:44 PM · cameramovementheightlooklinecast

Want to apply "height" to my DeadSpace Camera --> Physics.Linecast gives me trouble

Ok, I have a script that simulates a DeadSpace Third Person Camera. The script gets applied to the camera and the "target" is the player.

The script works fine, till i tried to apply "height". Some characters are bigger, some are smaller, therefore applying a value height makes sense. Sadly Physics.Linecast gives me strange results. I tried many things, but i can't find the error.

It seems like Physics.Linecast use the orgin model high, while camera and controls using the new height (origin height + variable height). Can someone help me out of this mess? Thx!

using UnityEngine; using System.Collections;

public class Camera_DeadSpace : MonoBehaviour {

public Transform target;

public float distance = 2.0f; public float height = 0.8f;

public float in_damping = 6.0f; public float out_damping = 3.0f;

public float sensitivityX = 150F; public float sensitivityY = 150F; public float minimumY = -60F; public float maximumY = 60F;

public float minDistance = 0.0f; public float maxDistance = 4.0f; public float smooth = 10.0f;

private bool showCrosshair = false; private GUISkin skin = null; private float rotationY = 0F; private float rotationX = 0F; private float currentDistance;

void Awake(){ currentDistance = distance; }

// Update is called once per frame void LateUpdate () { Screen.showCursor = false; Screen.lockCursor = true;

 Vector3 position = target.position;

//HERE IS THE PROBLEM !!! IF YOU USE THIS LINE IT KILLS THE SCRIPT!!! //position.y = height + target.position.y;

 Vector3 transpos, targetrot;
 Quaternion transrot;


 if (!Input.GetButton("Aim")){
     showCrosshair=true;
     float wantedDistance = distance;

     currentDistance = Mathf.Lerp (currentDistance, wantedDistance, out_damping * Time.deltaTime);

     rotationX += Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
     rotationY -= Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
     rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);


     transrot = Quaternion.Euler(rotationY, rotationX, 0) ;
     transpos = (Quaternion.Euler(rotationY, rotationX, 0)) * new Vector3(0.0f, 0.0f, -currentDistance) + position;

     targetrot = new Vector3(0, rotationX, 0);
 }
 else{
     showCrosshair=true;
     float wantedDistance = 0;

     currentDistance = Mathf.Lerp (currentDistance, wantedDistance, in_damping * Time.deltaTime);

     rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
     rotationY -= Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
     rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

     transrot = Quaternion.Euler(rotationY, rotationX, 0) ;
     transpos = (Quaternion.Euler(rotationY, rotationX, 0))  * new Vector3(0.0f, 0.0f, -currentDistance) + position;

     targetrot = new Vector3(0, rotationX, 0);
 }

 Vector3 desiredCameraPos = transpos;

 RaycastHit hit;
 //float distance2 = 0;

 if( Physics.Linecast( position, desiredCameraPos, out hit ) ){
     print("hit " + hit.distance);
     transpos = (Quaternion.Euler(rotationY, rotationX, 0))  * new Vector3(0.0f, 0.0f, -hit.distance)  + position;
 }

 transform.rotation = transrot;
 transform.position = transpos;
 target.localEulerAngles = targetrot;

}

// Use this for initialization void Start () {

} }

Comment
Add comment · Show 1
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 MiraiTunga · May 02, 2012 at 12:58 PM 0
Share

i just tried to use the same script you are using,and had the same problem ,i am relatively new to programming and usually use java , how do u add a minimum to hit.distance?

Thanks---not sure about the rules of posting here hope iam in the right place

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ravart · Mar 10, 2011 at 10:55 AM

I didn't find a solution, but a workaround! Add to hit.distance a minimum distance like 0.2f

Cheers

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Camera Height (y) 2 Answers

How to rotate a Rigidbody without rotating the gameObject? 1 Answer

Camera Stutter During Distance Correction 0 Answers

How do i on the y axis my camera from moving? 0 Answers

Unity Android Movement Problem 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges