Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 markpollard1 · May 20, 2017 at 09:17 AM · rotationtransformcamera-movementcamera rotate

Third person Camera stop it from going lower than the terrian

alt text using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Third_Camera : MonoBehaviour {
 
   public static Third_Camera Instance;
 
   public Transform Target;
 
 
   public float DistanceFromTarget = 20f;
   public float tempDistanceFromTarget = 20f;
   public float minY = 10;
   public float StartY = 0;
   public float maxY = 80;
   public float Yoffset = 2.0f;
   public float rotateSpeedLR = 0.5f;
   public float rotateSpeedUPDOWN = 0.5f;
 
   public float MinHeightAboveFloor = 1;
 
   public bool hittingTerrian = false;
 
   public float TerrianDistance = 1;
 
   public GameObject TestSpawn;
 
   public LayerMask layerMask;
 
   public LayerMask TerrianLayerMask;
   public float radius = 20;
 
   public float doubleClickTime = 0.2f;
   public float clickTime = 0;
   public float ScrollSpeed = 2f;
 
   private float angleY = 0;
   private float angleX = 0;
 
   private bool firstClick = false;
   private bool doubleClick = false;
 
   private Vector3 tempCameraPosition = Vector3.zero;
 
   public bool autoPan = false;
   public bool canSeePlayer = true;
 
   private BoxCollider terrianCollider;
 
   float rotation;
 
 
 
 
   void Awake()
   {
     Instance = this;
     Target =Transform.FindObjectOfType<Third_Motor>().transform;
     terrianCollider = GetComponent<BoxCollider>();
   }
 
   // Use this for initialization
   void Start () {
     transform.localEulerAngles = new Vector3(StartY * 2,0,0);
   }
   
   void Update()
   {
     if(Input.GetKeyDown(KeyCode.LeftAlt))
     {
       autoPan =! autoPan;
     }
   }
 
   // Update is called once per frame
   void LateUpdate(){
 
     ///this is the scroll wheel adjust amount
     float sw = Input.GetAxis("Mouse ScrollWheel");
 
     ///setting the distance from the target using the scroll wheel adjustment amount
     DistanceFromTarget -= sw * Time.deltaTime *ScrollSpeed;
   
     if(Input.GetKeyDown(KeyCode.B))
     {
       ResetCameraBehindPlayer();
     }
       
     if(Input.GetMouseButton(1) || autoPan || Input.GetMouseButton(2))
     {
       RotateCamera();
     }
 
     float floorDistance = GetFloorDistance();
 
     if(floorDistance <= MinHeightAboveFloor)
     {
       print( "reached min floor height");
 
      
     }
    
     
       ///always makes sure the position is the distance away from the play so we get a circle effect
       transform.position = GetTargetWthOffest(Yoffset) - transform.forward * DistanceFromTarget;
     
   
     CheckForCollision();
   }
 
   float GetPlayerDistanceFromCamera()
   {
     return (Vector3.Distance(transform.position,Target.position));
   }
 
 
   float GetFloorDistance()
   {
     RaycastHit hit;
 
     if(Physics.Raycast( transform.position, Vector3.down, out hit,TerrianLayerMask))
     {
      // print(hit.distance);
       Debug.DrawLine(transform.position, hit.point,Color.red);
       return (hit.distance);
     }
 
     return -1;
   }
 
   Vector3 GetTargetWthOffest(float YOffset)
   {
     Vector3 tempPosition = new Vector3(Target.position.x,Target.position.y + YOffset, Target.position.z); 
       return tempPosition;
   } 
 
   void RotateCamera()
   {
     angleY = Mathf.Clamp(angleY - Input.GetAxis("Mouse Y") * rotateSpeedUPDOWN, minY,maxY);
     angleX += Input.GetAxis("Mouse X") * rotateSpeedLR;
 
     transform.localEulerAngles = new Vector3(angleY,angleX,0);
   }
     
   void CheckForCollision()
   {
 
     float distance = GetPlayerDistanceFromCamera();
 
     RaycastHit hit;
     Ray ray = new Ray(transform.position, transform.forward);
 
 
     if(Physics.Raycast(ray,out hit, distance, layerMask))
     {
       
       if(hit.transform.name != "Hero")
       {
        //print("something in the way move closer");
 
        RaycastHit hit2;
 
         Ray Secondray = new Ray(ray.GetPoint(100000), -ray.direction);
         if(hit.collider.Raycast(Secondray, out hit2, 10000000))
         {
           transform.position = hit2.point;
         }
           
       }
      
     }
      
   }
     
 
   void ResetCameraBehindPlayer()
   {
     ////reset camera
     Vector3 findPlayersDirection = Target.eulerAngles;
 
     transform.localEulerAngles = new Vector3(findPlayersDirection.x, findPlayersDirection.y, 0);
     //transform.localEulerAngles = new Vector3(minY,0,0);
     doubleClick = false;
 
   }
 
 
 }


Hi I am using the above code to Make the number 1 in the picture work. The Camera rotates around the player using the x and y cords. I am trying to get the camera to rotate as in picture 2 where the camera will not go though the terrain. The camera will stay at a certain height when it gets to that height point away from the terrain. I have got all the ray casts in so i know how far the camera is away from the terrain but i cannot seam to limit the camera's y vector so that it does enter the floor.

Can anybody help?

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.

Follow this Question

Answers Answers and Comments

92 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

Related Questions

I want the camera, which is following my spaceship, to rotate to wherever the cursor is looking 0 Answers

Camera following player gameObject,Camera follows player, with script attached. 1 Answer

fixed rotation around object 1 Answer

Forward and back movements with a camera emulating an isometric view 1 Answer

Rotate Around a moving Object with constant Offset 1 Answer


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