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 Balckisworth · Mar 23, 2011 at 06:20 PM · cameratoggle

Problem with changing target of Camera.

Hi, I'm having a problem with "toggling" between targets of my camera. The camera is meant to follow a GameObject, and when certain conditions are filled (not completely ready yet on that part, still needs length and direction if-statements) it should go from my 3rd Person Controller to a car, and also hide the character. The problem is, that it won't toggle between targets, or it will toggle one time (if i choose the car in the beginning as target, i can toggle to the 3ps controller, however if i choose 3ps controller in the beginning, it won't toggle at all.

Example: Target: catamount, i start the game, i drive around, i press "e", camera starts following the 3ps controller and Debug.Logs "E-key pressed while in car". Example2: Target: 3ps controller, i start the game, i run around, i press "e", camera won't change and Debug.Logs "E-key pressed while in car".

Here's the code, it's C#:

using UnityEngine; using System.Collections;

public class HackNSlashCamera : 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; public static bool goCar = false;

 private Transform _myTransform;
 private float x;
 private float y;
 private int pos;
 private GameObject nextTarget;

 void Awake() {
     _myTransform = transform;
 }

 // Use this for initialization
 void Start () {
     if(target == null)
         Debug.LogWarning("We do not have a target");
     else {
         CameraSetup();  
     }
 }
 void Update() {
     if (goCar == false) {
         if (Input.GetKeyDown (KeyCode.E)) {
             Debug.Log("E-key pressed");
             //var pos = target.transform.position;
             //pos.x = 1000;
             //target.transform.position = pos;
             nextTarget = GameObject.Find("catamount");
             target = nextTarget.transform;
             goCar = true;
         }
     }
     if (goCar == true) {
         if (Input.GetKeyDown (KeyCode.E)) {
             Debug.Log("E-key pressed while in car");
             nextTarget = GameObject.Find("Character");
             target = nextTarget.transform;
             goCar = false;
         }
     }
 }

 void LateUpdate() {
     if(Input.GetMouseButton(1) && target != null && goCar == false) {
         Debug.Log("Right Button is down");

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


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

         transform.rotation = rotation;
         transform.position = position;
     }
     else {
         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);
 }       

}

Help would be really appreciated (the main problem with the camera in first place, how to hide the character instead of sending it to x-positon 1000 is not necessary)

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
1
Best Answer

Answer by Owen-Reynolds · Mar 23, 2011 at 08:53 PM

If-else problem in your Update. Change the second if(goCar==true) to just else.

Both ifs are triggering, so the 2nd always snaps it back to the player.

Comment
Add comment · Show 2 · 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 Balckisworth · Mar 24, 2011 at 12:48 PM 0
Share

Oh... that's obvious. Still a little new to unity, and thought that the engine would convert the overflow if's automatically to "else ifs".

Thank you! ^^

avatar image Owen-Reynolds · Mar 24, 2011 at 03:17 PM 0
Share

I was just telling my guys, when a robot riding an N-dimensional cyborg chicken makes a PB&J, it still needs to watch the peanut butter doesn't $$anonymous$$r the bread. Past all the LookAt's and transform.up's, it's all just regular program$$anonymous$$g.

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

No one has followed this question yet.

Related Questions

Camera Toggle script 1 Answer

Toggle between scripts with a script? 2 Answers

Toggling camera with one key,Toggle Camera with one key? 1 Answer

Camera toggle and position 1 Answer

Using The Same Input Key To Switch Between Two Camera Views 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