Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Roll-A-Ball Transform With Camera (Help)

Discussion in 'Scripting' started by wrm186, Jun 17, 2019.

  1. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    Hello!

    I am trying to learn some scripting in Unity, so I'm trying to follow some basic tutorials on creating a simple roll-a-ball game. And then hopefully expand on it as I learn it more. I followed the tutorial that Unity has up on youtube, however the camera isn't working how I want it to. Thus I found another tutorial by Sebastian Lague for creating the camera how I would like to control the ball.

    In short, I want the ball to roll like the Unity tutorial has, but if I spin the camera in a different direction, I would like the ball to start rolling in that direction. Here is my attempt for trying to combine these scripts, if you could please help me on the correct way to combine/reference these two scripts together that would be great! And if possible, I would love to see the way on how you went about combining them so I can learn a little more.

    PlayerController:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.  
    9.     private Rigidbody rb;
    10.  
    11.     Transform cameraTransform;
    12.  
    13.     void Start ()
    14.     {
    15.         rb = GetComponent<Rigidbody>();
    16.         cameraTransform = Camera.main.transform;
    17.     }
    18.  
    19.     void FixedUpdate ()
    20.     {
    21.         float moveHorizontal = Input.GetAxis ("Horizontal");
    22.         float moveVertical = Input.GetAxis ("Vertical");
    23.  
    24.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    25.  
    26.         rb.AddForce (movement * speed) + cameraTransform.eulerAngles.y;
    27.     }
    28.  
    29. }
    30.  
    CameraController:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ThirdPersonCamera : MonoBehaviour
    6. {
    7.     public float mouseSensitivity = 10;
    8.     public Transform target;
    9.     public float dstFromTarget = 2;
    10.     public Vector2 pitchMinMax = new Vector2 (0, 85);
    11.  
    12.     public float rotationSmoothTime = .12f;
    13.     Vector3 rotationSmoothVelocity;
    14.     Vector3 currentRotation;
    15.  
    16.     float yaw;
    17.     float pitch;
    18.  
    19.  
    20.     void LateUpdate ()
    21.     {
    22.         yaw += Input.GetAxis ("Mouse X");
    23.         pitch -= Input.GetAxis ("Mouse Y");
    24.         pitch = Mathf.Clamp (pitch, pitchMinMax.x, pitchMinMax.y);
    25.  
    26.         currentRotation = Vector3.SmoothDamp (currentRotation, new Vector3 (pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
    27.  
    28.         transform.eulerAngles = currentRotation;
    29.  
    30.         transform.position = target.position - transform.forward * dstFromTarget;
    31.     }
    32.  
    33. }
    34.  
    Thank you!
    -Wade
     
    Last edited: Jun 17, 2019
  2. TPEUnity

    TPEUnity

    Joined:
    Jan 17, 2018
    Posts:
    36
    If i understood u correctly u wanted something like this.
    Replace PlayerController FixedUpdate with this. U probably want to do inputs on update and cache camera instead of calling Camera.main since its pretty slow and considered bad habit usually.

    Hope this gets u on right track.

    Code (CSharp):
    1.  
    2.         Vector3 v = Input.GetAxis("Vertical") * Camera.main.transform.forward;
    3.         Vector3 h = Input.GetAxis("Horizontal") * Camera.main.transform.right;
    4.         var moveDir = (v + h).normalized;
    5.         float m = Mathf.Abs(Input.GetAxis("Horizontal")) + Mathf.Abs(Input.GetAxis("Vertical"));
    6.         var moveAmount = Mathf.Clamp01(m);
    7.  
    8.         rb.AddForce(moveDir * moveAmount * speed);
     
  3. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    Hey TPEUnity!

    Sorry for the late reply, but I just tried the one you provided and it worked perfectly! Thank you so much for the help!

    As I am trying to get a better understanding of your fix, could you please answer two questions if you've got the time?

    1. I see you put "float m" and "var" in the Update function, I always thought these were supposed to go before the Start function so you can reference these later? In short, I guess what is the letter "m" referencing since I don't have it defined above?

    2. I changed FixedUpdate to just Update, is this what you're meaning? As for doing cache camera I will need to do a little more research on how to do that. Thank you for the tip though!

    -Wade
     
  4. TPEUnity

    TPEUnity

    Joined:
    Jan 17, 2018
    Posts:
    36
    1. You could definetely declare these as instance variables i just kept it short for the sake of example. If you called these at Awake/Start method they would be only called ONCE and thats it. Since u want constant movement based on input u want this to happen every Update/FixedUpdate.

    This value represents the combined input amount that goes from 0 to 1 and as u can see its used as multiplier on the movement vector. Imagine it as quick 0% to 100% speed slider. Also maybe the keyword "var" confused u a little so here is post about it https://stackoverflow.com/questions/4307467/what-does-var-mean-in-c
    Not exactly. U should get input values in Update and do the physics stuff on FixedUpdate. I wont go into Update vs FixedUpdate since internet is already full of well detailed posts of this but in short Update gets called every frame and FixedUpdate gets called every physics timestamp.

    Heres some reading material https://stackoverflow.com/questions...ifference-between-update-fixedupdate-in-unity
     
  5. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    Okay, I think I am understanding what you're saying now. Most of it makes pretty good sense anyways. Still needing to understand all those match functions more though lol. I'll be sure to make the edit to the script for the Update and Fixed Update methods too, after reading some more on the topics I understand it a little more now too.

    And thank you for linking that article about the "var", I think I understand it a little more, may need to read up on it more to fully understand it and how to properly use it haha. Anything you link helps though at this point, as I don't really know anything haha.

    I appreciate your help again!