Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

how to change ball axis/controls based on directions?

Discussion in 'Editor & General Support' started by FiaKun, Jan 16, 2022.

  1. FiaKun

    FiaKun

    Joined:
    Sep 20, 2020
    Posts:
    2
    hi, im working on racing game, but instead of cars/kart it's more like hamsterball race.
    i've worked through the kart microgame template, create new scene where i can test it, and manage to get cinemachine work with directions with unefficient scripting and still on default control of 'roll a ball' control(absolute x,z axis). Thanks in advance!

    heres a demo video

    here's code for bottom :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FollowSpere : MonoBehaviour
    6. {
    7.    
    8.     public Transform followSpehre;
    9.     public float turnSmoothTime = 1.5f;
    10.  
    11.     float turnSmoothVelocity;
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         FollowSpherePosition();
    17.         Direction();
    18.  
    19.     }
    20.  
    21.     public void Direction()
    22.     {
    23.        
    24.         float horizontal = Input.GetAxisRaw("Horizontal");
    25.         float vertical = Input.GetAxisRaw("Vertical");
    26.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    27.  
    28.         if (direction.magnitude >= 0.1f)
    29.         {
    30.  
    31.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
    32.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    33.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    34.  
    35.         }
    36.     }
    37.  
    38.     private void FollowSpherePosition()
    39.     {
    40.         transform.position = followSpehre.transform.position;
    41.     }
    42.  
    43. }
    44.  

    And here's code for ball :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallController : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.     public float moveSpeed = 10f;
    9.  
    10.     private float xInput;
    11.     private float zInput;
    12.    
    13.     // Start is called before the first frame update
    14.     void Awake()
    15.     {
    16.         rb = GetComponent<Rigidbody>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         ProcessInputs();
    23.  
    24.  
    25.  
    26.     }
    27.  
    28.     private void FixedUpdate()
    29.     {
    30.         Move();
    31.     }
    32.  
    33.     private void ProcessInputs()
    34.     {
    35.         xInput = Input.GetAxis("Horizontal");
    36.         zInput = Input.GetAxis("Vertical");
    37.     }
    38.  
    39.     private void Move()
    40.     {
    41.         rb.AddForce(new Vector3(xInput, 0f, zInput) * moveSpeed);
    42.     }
    43.  
    44. }
    45.  

    i know it's a long read, and the scripts are unefficient

    cinemachine setup reference
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    24,679
    You're welcome to look at the DemoCameraRotatedControls scene in my proximity buttons package.

    The relevant script is here:

    https://github.com/kurtdekker/proxi...edControls/RotatedControlsPlayerController.cs

    Look around line 124 to line 130 for where the world inputs are rotated based on camera heading.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/
     
  3. FiaKun

    FiaKun

    Joined:
    Sep 20, 2020
    Posts:
    2
    thanks for suggestion, i've looked into it

    but is there's a way to compile it with rb.AddForce??
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    24,679
    The same principle applies. You have X/Z (or X/Y) in world inputs, you want them in local. The multiply by camera heading does that. What you do with the inputs after that is up to you.
     
unityunity