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 awplays49 · Dec 28, 2014 at 03:56 PM · strange

Trying to make a third person camera, whats wrong with this transform.position?

im trying to make it so that if the mouse is moved up, the camera moves back, moves down, and rotates up. and the opposite when the mouse moves down. This variable is applied to the localposition:

 CameraPos = new Vector3 (transform.localPosition.x, Mathf.Clamp (CameraPos.y + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.position.y + CameraTarget.transform.localScale.y / 2, CameraTarget.transform.position.y + CameraTarget.transform.localScale.y / 2 + MaxPos), Mathf.Clamp (CameraPos.z + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.position.z - MaxPos, CameraTarget.transform.position.z));

try making a player, making the camera a child, and applying this script to the camera:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
     
     public float Sensitivity;
     public float MaxPos;
     public float MaxAngle;
     private float MouseY;
     private float MouseYSet;
     private float CameraAngle;
 
     private Vector3 CameraPos;
 
     public GameObject CameraTarget;
     
     void Start () {
         
     }
     
     void Update () {
         // Rotation controls
         MouseY -= Input.GetAxisRaw ("Mouse Y");
         CameraAngle = Mathf.Clamp (CameraAngle + (MouseY - MouseYSet) * Sensitivity * 10, -MaxAngle, MaxAngle);
         CameraPos = new Vector3 (transform.localPosition.x, Mathf.Clamp (CameraPos.y + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.position.y + CameraTarget.transform.localScale.y / 2, CameraTarget.transform.position.y + CameraTarget.transform.localScale.y / 2 + MaxPos), Mathf.Clamp (CameraPos.z + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.position.z - MaxPos, CameraTarget.transform.position.z));
         if (MouseY != 0)
         {
             transform.localRotation = Quaternion.Euler (CameraAngle, 0, 0);
             transform.localPosition = new Vector3 (0, CameraPos.y, CameraPos.z);
             MouseYSet = MouseY;
         }
     }
 }

and this to the player:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public int Speed;
     public int RunSpeed;
     public int Sensitivity;
     public int JumpHeight;
     public int Force;
 
     private float TerrainHeight;
     private float TerrainHeightSet;
     private float PlayerFoot;
     private float MouseX;
     private float MouseXSet;
 
     private bool Jumping;
     private bool Running;
 
     void Start () {
         Jumping = false;
         Running = false;
     }
 
     void Update () {
         PlayerFoot = transform.position.y - transform.lossyScale.y / 2;
         TerrainHeight = Terrain.activeTerrain.SampleHeight (transform.position);
 
         // Translation controls
         if (Input.GetKey (KeyCode.W) && Running == false)
         {
             transform.position += transform.forward * Speed * 20 * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.S) && Running == false)
         {
             transform.position -= transform.forward * Speed * 20 * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.D) && Running == false)
         {
             transform.position += transform.right * Speed * 20 * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.A) && Running == false)
         {
             transform.position -= transform.right * Speed * 20 * Time.deltaTime;
         }
 
         // Run controls
         if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.Q))
         {
             Running = true;
         }
         if (Input.GetKey (KeyCode.W) && Running == true)
         {
             transform.position += transform.forward * RunSpeed * 20 * Time.deltaTime;
         }
         if (Input.GetKeyUp (KeyCode.Q) && Input.GetKeyUp (KeyCode.W))
         {
             Running = false;
         }
 
         // Rotation controls
         MouseX += Input.GetAxisRaw ("Mouse X");
 
         if (MouseXSet != MouseX)
         {
             transform.Rotate (new Vector3 (0,(MouseX - MouseXSet) * Sensitivity * 10, 0));
             MouseXSet = MouseX;
         }
 
         // Jump controls
         if (Input.GetKey (KeyCode.Space) && PlayerFoot < TerrainHeight + 1 && PlayerFoot < TerrainHeight + JumpHeight * 10)
         {
             TerrainHeightSet = TerrainHeight;
             StartCoroutine (JumpDelay ());
         }
         if (Jumping == true)
         {
             rigidbody.velocity = new Vector3 (0, (Force * 10), 0);
         }
         if (Input.GetKeyUp (KeyCode.Space))
         {
             Jumping = false;
         }
         if (PlayerFoot >= TerrainHeightSet + JumpHeight * 10)
         {
             Jumping = false;
         }
         if (Jumping == false)
         {
             rigidbody.velocity = new Vector3 (0, -(Force * 20), 0);
         }
 
         // Keep player upright
         if (PlayerFoot < TerrainHeight + 1)
         {
             transform.position += Vector3.up * (TerrainHeight - PlayerFoot);
         }
     }
 
     // Check if player really wants to jump
     IEnumerator JumpDelay () {
         yield return new WaitForSeconds (0.05f);
         if (Input.GetKey (KeyCode.Space))
         {
             Jumping = true;
         }
         StopCoroutine (JumpDelay ());
     }
 } 





Comment
Add comment · Show 3
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 awplays49 · Dec 28, 2014 at 03:57 PM 0
Share

I forgot to mention, what it is doing is putting the camera on the ground, which made me think that the localscale / 2 part would help, but it didnt.

avatar image Maurice_B · Dec 28, 2014 at 04:20 PM 0
Share

what doesnt work??

avatar image awplays49 · Dec 28, 2014 at 08:25 PM 0
Share

The transform.localPosition part. in the first script. it just puts the camera on the ground behind the characters feet

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by WillNode · Dec 29, 2014 at 07:34 AM

change your Camera localPosition with this code :

 float distance=4f;
 
 void Update(){
 transform.position = transform.rotation * new Vector3 (0.0f, 0.0f, -distance) + CameraTarget.transform.position;
 }

by using this code you don't need to make parent to your camera.

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

28 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

Related Questions

Parsing error called but can't figure it out. please help! 0 Answers

Why is iOS app size 10x larger than Android? 1 Answer

Soo wierd!!! Clamp doesnt work when moving and goes past clamp point! 1 Answer

Physics.OverlapSphere returns a strange "centerSphere" collider? 1 Answer

Input Scroll Wheel And Vertical Axis Work Together? 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