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 /
avatar image
0
Question by Liraseth · Oct 06, 2019 at 09:55 PM · cameramovementplayerplayer movementrelative movement

Player Movement Relative to Camera?

Hi all... for now my charter (in my case a ball) moves relative to the world and not the camera. How is it possible to make the ball move relative to the camera rather than the world?!

Here is my script...

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerControl : MonoBehaviour

{

 public float speed;
 public float jump = 20f;
 public Transform SpawnPoint;
 private Rigidbody rb;
 float fallZone = -20f;
 bool isGrounded = true;
 bool isSpawn = true;
 float forward;
 GameObject MainCamera;
    
 void Start()
 {
     rb = GetComponent<Rigidbody>();
     MainCamera = GameObject.Find("Main Camera");
 }

 void FixedUpdate()
 {
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");
     
     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

     rb.AddRelativeForce(movement*speed);
 }

 private void Update()
 {
     bool player_jump = Input.GetButtonDown("Jump");

     if (player_jump && isGrounded)
     {
         rb.AddForce(Vector3.up * jump);
     }

     if (rb.transform.position.y < fallZone)
     {
         rb.transform.position = SpawnPoint.transform.position;
         rb.velocity = Vector3.zero;
         rb.angularVelocity = Vector3.zero;
     }
 }

 void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.CompareTag("Ground"))
     {
         isGrounded = true;
     }      

     if (!SpawnPoint)
     {
         isSpawn = true;
     }

     if (collision.gameObject.CompareTag("Finish"))
     {
         Application.LoadLevel("Level_2");
     }
 }
       
 void OnCollisionExit(Collision collision)
 {
     if (collision.gameObject.CompareTag("Ground"))
     {
         isGrounded = false;
     }

     if (!SpawnPoint)
     {
         isSpawn = true;
     }
 }

}

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Liraseth · Oct 09, 2019 at 07:04 PM

Ok I found this to work well with the help of other posts. Make sure that at the start of the script there is...

 public float speed;
 private Rigidbody rb;

 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }

then the main part...

 void FixedUpdate()
 {
     Vector3 movement;

     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");

     GameObject cameraCompass = new GameObject("Camera Compass");
     // "Correct" the compass so that it lies flat in the game plane
     cameraCompass.transform.eulerAngles = new Vector3(0, GameObject.FindGameObjectWithTag("MainCamera").transform.eulerAngles.y, 0f);

     Vector3 movementX = cameraCompass.transform.right * moveHorizontal;
     Vector3 movementZ = cameraCompass.transform.forward * moveVertical;
     movement = movementX + movementZ;

     rb.AddForce(movement * speed);
     
     Destroy(cameraCompass); // We don't need the Compass anymore!
 }
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
2

Answer by visca_c · Oct 08, 2019 at 02:00 PM

I assume you want your camera to follow the target. The easiest way is to attach your camera to your character(which is to make it a child object of the character). You can do that by going into editor hierarchy view, drag the camera, and drop it onto your character.

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
2

Answer by GubaLord · Oct 08, 2019 at 02:09 PM

I am not sure if it's that you want, but try to multiply the movement with the rotation of the camera. Right before you add it as force.

 movement = MainCamera.transform.rotation * movement;
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 Liraseth · Oct 09, 2019 at 05:36 PM 0
Share

When i put this in the code, the ball was struggling to know what it needs to do so it jest rolls back and forward

avatar image
1

Answer by gusbmurphy · Oct 08, 2019 at 03:51 PM

I'm working on a little project where I think I've tackled a similar problem to what you're looking at. I'm using the WASD keys instead of an input axis, but I have a camera that follows the player, and I use this method to get the up, down, left and right directions relative to the camera:

     private void SetUpCameraDirections()
     {
         GameObject cameraCompass = new GameObject("Camera Compass");
         // "Correct" the compass so that it lies flat in the game plane
         cameraCompass.transform.eulerAngles = new Vector3(0, GameObject.FindGameObjectWithTag("MainCamera").transform.eulerAngles.y, 0);
 
         cameraForward = cameraCompass.transform.forward;
         cameraBack = -cameraCompass.transform.forward;
         cameraRight = cameraCompass.transform.right;
         cameraLeft = -cameraCompass.transform.right;
 
         Destroy(cameraCompass); // We don't need the Compass anymore!
     }

Wow, I wish these boards would format code better! Anyway, I call that method in the Start method of the script on the object my player is controlling. It's creating that "camera compass" that lies flat on the 2D plane that movement happens on, and is rotated to look in the direction that the MainCamera is looking. I then use those cameraForward, Back, Right and Left vectors to move the player. Hope this is helpful!

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 Liraseth · Oct 09, 2019 at 05:36 PM 0
Share

Unity says camera'direction' does not exist in the current context?!?!

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

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

How to prevent player from moving in air while jumping in place?? 0 Answers

Non slippery movement 1 Answer

How to make the player move where camera is facing? 1 Answer

Problem with camera and character controller (fighting game) 1 Answer

Third-Person Camera keeps intersecting with terrain/buildings 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