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 Sameer34 · Sep 19, 2020 at 05:51 AM · cameraplayer movementrelative

How to give camera relative movement to the player and I am using joystick for player and camera

My Player Script is =

using System.Collections; using System.Collections.Generic; using static System.Diagnostics.Debug; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using static UnityEngine.Debug;

public class Ball : MonoBehaviour { // Player Movement Controls

 protected Joystick Leftjoystick;
 //protected Joybutton joybutton;

 // Start is called before the first frame update
 void Start()
 {
     Leftjoystick = FindObjectOfType<Joystick>();
     //joybutton = FindObjectOfType<Joybutton>();

 }

 // Update is called once per frame
 void Update()
 {
     var rigidbody = GetComponent<Rigidbody>();

     rigidbody.velocity = new Vector3(Leftjoystick.Horizontal * 30f, rigidbody.velocity.y, Leftjoystick.Vertical * 30f);
 }

}

My Camera Script is =

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

public class FreeCamera : MonoBehaviour { public VirtualJoystick cameraJoystick;

 public Transform lookAt;

 private float distance = 20.0f;
 private float currentX = 0.0f;
 private float currentY = 20.0f;
 private float sensivityX = 3.0f;
 private float sensivityY = 1.0f;

 private void Update()
 {
     currentX += cameraJoystick.InputDirection.x * sensivityX;
     currentY += cameraJoystick.InputDirection.z * sensivityY;
 }

 private void LateUpdate()
 {
     Vector3 dir = new Vector3(0, 0, -distance);
     Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
     transform.position = lookAt.position + rotation * dir;
     transform.LookAt(lookAt);
 }

}

My Joystick script is =

using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems;

public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler {

 private Image jsContainer;
 private Image joystick;

 public Vector3 InputDirection;

 void Start()
 {

     jsContainer = GetComponent<Image>();
     joystick = transform.GetChild(0).GetComponent<Image>(); //this command is used because there is only one child in hierarchy
     InputDirection = Vector3.zero;
 }

 public void OnDrag(PointerEventData ped)
 {
     Vector2 position = Vector2.zero;

     //To get InputDirection
     RectTransformUtility.ScreenPointToLocalPointInRectangle
             (jsContainer.rectTransform,
             ped.position,
             ped.pressEventCamera,
             out position);

     position.x = (position.x / jsContainer.rectTransform.sizeDelta.x);
     position.y = (position.y / jsContainer.rectTransform.sizeDelta.y);

     float x = (jsContainer.rectTransform.pivot.x == 1f) ? position.x * 2 + 1 : position.x * 2 - 1;
     float y = (jsContainer.rectTransform.pivot.y == 1f) ? position.y * 2 + 1 : position.y * 2 - 1;

     InputDirection = new Vector3(x, y, 0);
     InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;

     //to define the area in which joystick can move around
     joystick.rectTransform.anchoredPosition = new Vector3(InputDirection.x * (jsContainer.rectTransform.sizeDelta.x / 3)
                                                            , InputDirection.y * (jsContainer.rectTransform.sizeDelta.y) / 3);

 }

 public void OnPointerDown(PointerEventData ped)
 {

     OnDrag(ped);
 }

 public void OnPointerUp(PointerEventData ped)
 {

     InputDirection = Vector3.zero;
     joystick.rectTransform.anchoredPosition = Vector3.zero;
 }

}

Comment
Add comment · Show 1
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 Durstie · Sep 19, 2020 at 01:18 PM 0
Share

Can you edit your question so that there is a blank line before each code block? I think that should make the code more properly formatted and easier to read.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheonlysiQ · Sep 19, 2020 at 09:25 PM

Make the camera a child that belongs to the player. Then, when you move the player, the camera will follow. Offset the initial transform coordinates to get the camera position you need.

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
avatar image
0

Answer by Sameer34 · Sep 24, 2020 at 07:56 AM

I have Solved my problem by

By using new game object and added same script

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

211 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 avatar image avatar image avatar image avatar image

Related Questions

Need help for player rotation and camera rotation 0 Answers

How do you move the camera relative to the direction it is currently facing? 1 Answer

GameObjects don't render every few frames 1 Answer

Calculate screen top and bottom postion 1 Answer

Can relative velocity be calculated from relative direction vector 3 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