Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 TOXON · May 02, 2016 at 06:39 PM · ballcontrolrelative rotationspherical

Rolling ball spherical terrain relative controls - C#

alt text

Ok I takes everything from the beginning.

I have a camera following my player (a rolling ball) around a planet with a gravity script. Everything works fine, except for the controls. Depending on where I am on the planet directions are reversed, they do anything ...

Below the script controller. I tried all full of things, nothing works. The controls are not related to the camera .... How to do that?

Thanks for your help.

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.CrossPlatformInput;
 
 public class PlayerController : MonoBehaviour
 {
 
     PhotonView view;
     public Ball ball;
 
     private Transform cam; // A reference to the main camera in the scenes transform
     private Vector3 camForward; // The current forward direction of the camera
     private bool jump; // whether the jump button is currently pressed
     private Vector3 move; // the world-relative desired move direction, calculated from the camForward and user input.
 
     private void Awake()
     {
         // Set up the reference.
         ball = GetComponent<Ball>();
 
         // get the transform of the main camera
         if (Camera.main != null)
         {
             cam = Camera.main.transform;
 
             //cam = cam.transform;
         }
         else
         {
             Debug.LogWarning(
                 "Warning: no main camera found. Ball needs a Camera tagged \"MainCamera\", for camera-relative controls.");
             // we use world-relative controls in this case, which may not be what the user wants, but hey, we warned them!
         }
     }
 
     // Use this for initialization
     void Start()
     {
         view = GetComponent<PhotonView>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (view.isMine)
         {
             // Get the axis and jump input.
 
             float h = CrossPlatformInputManager.GetAxis("Horizontal");
             float v = CrossPlatformInputManager.GetAxis("Vertical");
             jump = CrossPlatformInputManager.GetButton("Jump");
 
 
             // calculate move direction
             if (cam != null)
             {
                 // calculate camera relative direction to move:
 
 
 
                 camForward = Vector3.Scale(cam.forward, new Vector3(3, 0, 1)).normalized;
                 move = (v * camForward + h * cam.right).normalized;
                 //move = (v * Vector3.forward + h * Vector3.right).normalized;
             }
             else
             {
                 // we use world-relative directions in the case of no main camera
                 //move = (v * Vector3.forward + h * Vector3.right).normalized;
                 Debug.Log("no camera");
             }
 
             Vector3 movement = new Vector3(h, 0.0f, v);
             movement = Camera.main.transform.TransformDirection(movement);
 
             ball.Move(move, jump);
             jump = false;
         }
 
     }
 
     void FixedUpdate()
     {
        
 
     }
 }
 

And The ball script :

 using System;
 using UnityEngine;
 using UnityStandardAssets.CrossPlatformInput;
 
 public class Ball : MonoBehaviour
     {
         [SerializeField]
         private float m_MovePower = 5; // The force added to the ball to move it.
         [SerializeField]
         private bool m_UseTorque = false; // Whether or not to use torque to move the ball.
         [SerializeField]
         private float m_MaxAngularVelocity = 25; // The maximum velocity the ball can rotate at.
         [SerializeField]
         private float m_JumpPower = 2; // The force added to the ball when it jumps.
 
         private const float k_GroundRayLength = 1f; // The length of the ray to check if the ball is grounded.
         private Rigidbody m_Rigidbody;
 
 
         private void Start()
         {
             m_Rigidbody = GetComponent<Rigidbody>();
             // Set the maximum angular velocity.
             GetComponent<Rigidbody>().maxAngularVelocity = m_MaxAngularVelocity;
         }
 
 
         public void Move(Vector3 moveDirection, bool jump)
         {
             // If using torque to rotate the ball...
             if (m_UseTorque)
             {
                 // ... add torque around the axis defined by the move direction.
                 m_Rigidbody.AddTorque(new Vector3(moveDirection.z, 0, -moveDirection.x) * m_MovePower);
          
         }
             else
             {
             // Otherwise add force in the move direction.
             
             m_Rigidbody.AddForce(moveDirection * m_MovePower);
             }
 
             // If on the ground and jump is pressed...
             if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
             {
                 // ... add force in upwards.
                 m_Rigidbody.AddForce(Vector3.up * m_JumpPower, ForceMode.Impulse);
             }
         }
     }
 

000.jpg (50.4 kB)
sans-titre-2.jpg (59.6 kB)
Comment
Add comment · Show 2
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 Baste · May 03, 2016 at 01:28 PM 0
Share

You forgot to include the question. What are you not able to do? What's not working?

Can't help you if we don't know what you need help with.

avatar image TOXON Baste · May 03, 2016 at 04:31 PM 0
Share

-_- Yes it's not clever. I tried to explain better my problem. Thank you

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how can i make drag and release (like billiard games) ball control 1 Answer

Can anyone please tell what to do make a bomb explode on touching ? 1 Answer

How do I make sure my character is always perpendicular to the surface when using NavMesh? 0 Answers

movement control with mouse clicks 0 Answers

How can i move my player automatically? 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