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 thunderclaws · Sep 11, 2015 at 08:29 AM · getcomponentthird-personthirdpersoncontrollerthird-person-camera

need general help with camerascript

okay here is the script for my camera currently it has everything i need it to do, but the problem is that i want the vector 3 relpos (relative position) sent to my movescript, what i want it to do there is to determine the forward direction for the character. you know when you have a third person game you rotate your camera around your character and you press W he starts walking in the direction the camera is facing. so if anyone has any tips on: getting this script to make friends and communicate. make it function the way i want. cleaning up this nightmare of badly placed variables. thank you.

 using UnityEngine;
 using System.Collections;
 
 public class Camerascript : MonoBehaviour {
 
     float xrot = 1.5f;
     float yrot = -1.0f;
     float newxrot = 1.5f;
     float newyrot = -1.0f;
 
     float z;
     float x1;
     float x2;
     float y;
     float posy;
     float posx;
     float posz;
     float camspeedx;
     float camspeedy;
     float camspeedw;
 
 
     float forwards;
     Vector3 campos;
 
     public Transform player;
 
     //settings
 
     public float zoom = 3.0f;
     public float zoom2 = 3.0f;    
     public float sensitivity = 0.5f;
     public float smooth = 2f;
     public string targetplayer = "Cube";
     public string XInput = "Mouse X";
     public string YInput = "Mouse Y";
     public float Xmultiplier = 1.0f;
     public float Ymultiplier = 1.0f;
 
     public Vector3 relpos;
 
 
     void Start () {
     }
 
 
     void Update () {
 
         //player position
         GameObject go = GameObject.Find (targetplayer);
         player = go.GetComponent <Transform> ();
 
         //zoom
         int layerMask = 1 << 8;
         layerMask = ~layerMask;
         int layerMask2 = 1 << 9;
         layerMask2 = ~layerMask2;
 
         RaycastHit hit;
         RaycastHit hit2;
         zoom2 -= Input.GetAxis ("ScrollWheel") * 5.0f;
 
         if (Physics.Raycast (player.transform.position, transform.position, out hit, zoom +1.5f, layerMask)) {
             //if (Physics.SphereCast (player.transform.position, 0.2f, transform.position, out hit2, zoom, layerMask2)) {
             //    zoom = Mathf.Clamp (zoom2, 0.1f, hit2.distance);
             //}else{
                 zoom = Mathf.Clamp (zoom2, 0.1f, hit.distance);
             //}
         } else {
             zoom = Mathf.SmoothDamp(zoom, zoom2, ref camspeedw, 0.2f);
         }
 
         //input
         float camx = -Input.GetAxis (XInput);
         float camy = -Input.GetAxis (YInput);
 
         //rotation
         newxrot += sensitivity * Xmultiplier * camx;
         newyrot += sensitivity * Ymultiplier * camy;
         newyrot = Mathf.Clamp(newyrot, -3.0f, -0.2f); 
         xrot = Mathf.SmoothDamp(xrot, newxrot, ref camspeedx, Time.deltaTime * smooth);
         yrot = Mathf.SmoothDamp(yrot, newyrot, ref camspeedy, Time.deltaTime * smooth);
 
         //orientation
         z = Mathf.Sin (xrot);
         x1 = Mathf.Cos (xrot);
         x2 = Mathf.Sin (yrot);
         y = Mathf.Cos (yrot);
     
         Relpos (); 
 
         //direction
         transform.LookAt(player);
 
     }
     public class Test{
         static float TopKek = 9001.0f;
 
     }
 
     public void Relpos()
     { 
         Vector3 position;
         relpos.y = player.position.y + y * zoom;
         relpos.x = player.position.x + x1 * x2 * zoom;
         relpos.z = player.position.z + z * x2 * zoom;
         position.x = relpos.x;
         position.y = relpos.y;
         position.z = relpos.z;
         transform.position = position;
     }
 }
 


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

27 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

Related Questions

Character jitters when Moving my camera 0 Answers

Third Person Controller moving in direction of camera 0 Answers

Camera for a Third Person Shooter dosen't work 1 Answer

Thirdperson camera follow ball 1 Answer

,How to make the charater rotate 180 with motion and play the idle in the good direction at the end of the motion 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