Search Unity

Ball roll control / transform.localscale. Time.deltatime

Discussion in 'Physics' started by Kiesco91, Mar 10, 2019.

  1. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    hello,

    I have 2 questions. seemed easier to link them into 1 thread as they are simple fixes that I can't seem to get right.

    BALL CONTROL:

    I'm creating a game where you roll a ball from start to finish (basic I know but focusing on learning scripting not gameplay). when the ball rolls around the board and then you stop the joystick, it just stops dead rather than rolling around like a ball naturally would. I feel like I should be using AddForce to make the ball feel more like a ball but can't seem to find out where to add it in.

    my current script is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     protected Joystick joystick;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         joystick = FindObjectOfType<Joystick>();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         var rigidbody = GetComponent<Rigidbody>();
    19.  
    20.         rigidbody.velocity = new Vector3(joystick.Horizontal * 10f,
    21.         rigidbody.velocity.y,
    22.             joystick.Vertical * 10f);
    23.  
    24.     }
    25. }
    SECOND ISSUE:

    I also want the ball to slowly scale up in size over the duration of the level. I have it working where it scales using Time.deltaTime but is scaling up too quickly and can't figure out how to slow it down. can I slow down Time.deltaTime or is there another alternative?

    That script is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerGrow : MonoBehaviour
    6. {
    7.  
    8.     Vector3 scale;
    9.  
    10. void Update()
    11.     {
    12.         scale = transform.localScale;
    13.  
    14.         scale.x += Time.deltaTime;
    15.         scale.y += Time.deltaTime;
    16.         scale.z += Time.deltaTime;
    17.  
    18.         transform.localScale = scale;
    19.     }
    20. }
    21.  

    thanks in advance!
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    1) change Update to FixedUpdate, uts the physics update, and just say rigidbody.AddForce with you vector.
    Might need AddRelativeForce or rotate the input vector by the ball rotation, but thats a bit more advanced.

    2) divide delta time by something to slow it down, divide by half to make it last 2 time as long, the opposite is true for multiplcation.
     
  3. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80

    hello,

    thank you for your reply, I tried changing rigidbody.velocity to rigidbody.AddForce and get errors. I have attached a picture of said errors.

    thanks,
     

    Attached Files:

  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536