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 /
  • Help Room /
avatar image
0
Question by Profiter · Jul 28, 2018 at 05:27 AM · camerafollow player

How to make camera distace and angle static following player?

Hi ,

i am trying to learn unity using some bike controller from asset store, however the camera has abit problem i cant solve, would appreciate if i could get some help

As you see my camera below is set from a distance and angle from the player, alt text

However when my player went uphill or downhill on slope, the camera position changed alt text

I want to know how could i set the camera statically following my player from an angle and position and not changing while my player go uphill or downhill? Thanks in advance !!!

 public class BikeCamera : MonoBehaviour
 {
 
 
 
     public Transform target;
     public Transform BikerMan;
 
     public float smooth = 0.3f;
     public float distance = 5.0f;
     public float height = 1.0f;
     public float Angle = 20;
 
 
     public List<Transform> cameraSwitchView;
     public BikeUIClass BikeUI;
 
     public LayerMask lineOfSightMask = 0;
 
    
 
 
 
     private float yVelocity = 0.0f;
     private float xVelocity = 0.0f;
     [HideInInspector]
     public int Switch;
 
     private int gearst = 0;
     private float thisAngle = -150;
     private float restTime = 0.0f;
 
 
     private Rigidbody myRigidbody;
 
 
 
     private BikeControl bikeScript;
 
 
 
     [System.Serializable]
     public class BikeUIClass
     {
 
         public Image tachometerNeedle;
         public Image barShiftGUI;
 
         public Text speedText;
         public Text GearText;
 
     }
 
 
     
 
 
     ////////////////////////////////////////////// TouchMode (Control) ////////////////////////////////////////////////////////////////////
 
 
     private int PLValue = 0;
 
 
     public void PoliceLightSwitch()
     {
 
         if (!target.gameObject.GetComponent<PoliceLights>()) return;
 
         PLValue++;
 
         if (PLValue > 1) PLValue = 0;
 
         if (PLValue == 1)
             target.gameObject.GetComponent<PoliceLights>().activeLight = true;
 
         if (PLValue == 0)
             target.gameObject.GetComponent<PoliceLights>().activeLight = false;
 
 
     }
 
 
     public void CameraSwitch()
     {
         Switch++;
         if (Switch > cameraSwitchView.Count) { Switch = 0; }
     }
 
 
     public void BikeAccelForward(float amount)
     {
        bikeScript.accelFwd = amount;
     }
 
     public void BikeAccelBack(float amount)
     {
         bikeScript.accelBack = amount;
     }
 
     public void BikeSteer(float amount)
     {
         bikeScript.steerAmount = amount;
     }
 
     public void BikeHandBrake(bool HBrakeing)
     {
         bikeScript.brake = HBrakeing;
     }
 
     public void BikeShift(bool Shifting)
     {
         bikeScript.shift = Shifting;
     }
 
 
 
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
     
 
     public void RestBike()
     {
 
         if (restTime == 0)
         {
             myRigidbody.AddForce(Vector3.up * 500000);
             myRigidbody.MoveRotation(Quaternion.Euler(0, transform.eulerAngles.y, 0));
             restTime = 2.0f;
         }
 
     }
 
 
 
 
     public void ShowBikeUI()
     {
 
 
 
         gearst = bikeScript.currentGear;
         BikeUI.speedText.text = ((int)bikeScript.speed).ToString();
 
 
 
 
         if (bikeScript.bikeSetting.automaticGear)
         {
 
             if (gearst > 0 && bikeScript.speed > 1)
             {
                 BikeUI.GearText.color = Color.green;
                 BikeUI.GearText.text = gearst.ToString();
             }
             else if (bikeScript.speed > 1)
             {
                 BikeUI.GearText.color = Color.red;
                 BikeUI.GearText.text = "R";
             }
             else
             {
                 BikeUI.GearText.color = Color.white;
                 BikeUI.GearText.text = "N";
             }
 
         }
         else
         {
 
             if (bikeScript.NeutralGear)
             {
                 BikeUI.GearText.color = Color.white;
                 BikeUI.GearText.text = "N";
             }
             else
             {
                 if (bikeScript.currentGear != 0)
                 {
                     BikeUI.GearText.color = Color.green;
                     BikeUI.GearText.text = gearst.ToString();
                 }
                 else
                 {
 
                     BikeUI.GearText.color = Color.red;
                     BikeUI.GearText.text = "R";
                 }
             }
 
         }
 
 
 
 
 
         thisAngle = (bikeScript.motorRPM / 20) - 175;
         thisAngle = Mathf.Clamp(thisAngle, -180, 90);
 
         BikeUI.tachometerNeedle.rectTransform.rotation = Quaternion.Euler(0, 0, -thisAngle);
         BikeUI.barShiftGUI.rectTransform.localScale = new Vector3(bikeScript.powerShift / 100.0f, 1, 1);
 
     }
 
 
 
     void Start()
     {
         target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
 
         bikeScript = (BikeControl)target.GetComponent<BikeControl>();
 
         myRigidbody = target.GetComponent<Rigidbody>();
 
         cameraSwitchView = bikeScript.bikeSetting.cameraSwitchView;
 
         BikerMan = bikeScript.bikeSetting.bikerMan;
 
     }
 
 
 
 
     void Update()
     {
 
         if (!target) return;
 
         //target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
 
         bikeScript = (BikeControl)target.GetComponent<BikeControl>();
 
         myRigidbody = target.GetComponent<Rigidbody>();
 
 
         if (Input.GetKeyDown(KeyCode.G))
         {
             RestBike();
         }
 
 
         if (Input.GetKeyDown(KeyCode.R))
         {
             Application.LoadLevel(Application.loadedLevel);
         }
 
 
         if (Input.GetKeyDown(KeyCode.E))
         {
             PoliceLightSwitch();
         }
 
 
         if (restTime!=0.0f)
         restTime=Mathf.MoveTowards(restTime ,0.0f,Time.deltaTime);
 
 
 
 
         ShowBikeUI();
 
         GetComponent<Camera>().fieldOfView = Mathf.Clamp(bikeScript.speed / 10.0f + 60.0f, 60, 90.0f);
 
 
 
         if (Input.GetKeyDown(KeyCode.C))
         {
             Switch++;
             if (Switch > cameraSwitchView.Count) { Switch = 0; }
         }
 
 
         if (!bikeScript.crash)
         {
             if (Switch == 0)
             {
                 // Damp angle from current y-angle towards target y-angle
 
                 float xAngle = Mathf.SmoothDampAngle(transform.eulerAngles.x,
                target.eulerAngles.x + Angle, ref xVelocity, smooth);
 
                 float yAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y,
                 target.eulerAngles.y, ref yVelocity, smooth);
 
                 // Look at the target
                 transform.eulerAngles = new Vector3(Angle, yAngle, 0.0f);
 
                 var direction = transform.rotation * -Vector3.forward;
                 var targetDistance = AdjustLineOfSight(target.position + new Vector3(0, height, 0), direction);
 
 
                 transform.position = target.position + new Vector3(0, height, 0) + direction * targetDistance;
 
 
             }
             else
             {
 
                 transform.position = cameraSwitchView[Switch - 1].position;
                 transform.rotation = Quaternion.Lerp(transform.rotation, cameraSwitchView[Switch - 1].rotation, Time.deltaTime * 5.0f);
 
             }
         }
         else
         {
             Vector3 look = BikerMan.position - transform.position;
             transform.rotation = Quaternion.LookRotation(look);
         }
 
     }
 
 
 
     float AdjustLineOfSight(Vector3 target, Vector3 direction)
     {
 
 
         RaycastHit hit;
 
         if (Physics.Raycast(target, direction, out hit, distance, lineOfSightMask.value))
             return hit.distance;
         else
             return distance;
 
     }
 
 
 }

up.png (307.3 kB)
normal.png (315.6 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by anurag7991 · Jul 28, 2018 at 11:44 AM

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CameraFollow : MonoBehaviour {

// attach the script to camera and choose how much distance you want in offset and drag the camera and attach it to your player

 public Transform target;
 public Vector3 Offset;

 void Update()
 {
     transform.position = target.position + Offset;
 }

}

or

using System.Collections; using System.Collections.Generic; using UnityEngine;

// choose values of x , y and z according to your platform inclination angle and size

public class CameraFollower : MonoBehaviour {

 public Transform lookat;
 public Vector3 offset;
 public Vector3 MoveVector;
 //public float transition;
 //public float AnimationDuration;
 //public Vector3 AnimationOffset = new Vector3 (0,200,200);

 // Use this for initialization
 void Start () 
 {
     lookat = GameObject.FindGameObjectWithTag ("Player").transform;
     offset = transform.position - lookat.position;
 }

 // Update is called once per frame
 void Update ()
 {
     transform.position = lookat.position + offset;
     MoveVector = lookat.position + offset;
     MoveVector.x = 0;
     MoveVector.y = Mathf.Clamp(MoveVector.y,100,200);

     if (transition > 1.0f)
     {
     */
     transform.position = MoveVector;
     /*
     }

     else 
     {

transform.position = Vector3.Lerp (MoveVector + AnimationOffset, MoveVector, transition); transition += Time.deltaTime * 1 / AnimationDuration;
transform.LookAt (lookat.position + Vector3.up); } }

 }

}

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 anurag7991 · Jul 28, 2018 at 11:45 AM 0
Share

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CameraFollower : $$anonymous$$onoBehaviour {

 public Transform lookat;
 public Vector3 offset;
 public Vector3 $$anonymous$$oveVector;
 //public float transition;
 //public float AnimationDuration;
 //public Vector3 AnimationOffset = new Vector3 (0,200,200);

 // Use this for initialization
 void Start () 
 {
     lookat = GameObject.FindGameObjectWithTag ("Player").transform;
     offset = transform.position - lookat.position;
 }

 // Update is called once per frame
 void Update ()
 {
     transform.position = lookat.position + offset;
     $$anonymous$$oveVector = lookat.position + offset;
     $$anonymous$$oveVector.x = 0;
     $$anonymous$$oveVector.y = $$anonymous$$athf.Clamp($$anonymous$$oveVector.y,100,200);

     if (transition > 1.0f)
     {
     */
     transform.position = $$anonymous$$oveVector;
     /*
     }

     else 
     {

transform.position = Vector3.Lerp ($$anonymous$$oveVector + AnimationOffset, $$anonymous$$oveVector, transition); transition += Time.deltaTime * 1 / AnimationDuration;
transform.LookAt (lookat.position + Vector3.up); } }

 }

}

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

207 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

Related Questions

Unity3D FollowCam not following player vehicle 0 Answers

camera problem orbit/fallow 0 Answers

How do I assign MainCamera to a dynamically created primitive? 0 Answers

camera falling down when following player!! 1 Answer

How I can make camera rotate smoothly followed object? 2 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