Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Help Wanted How can I make the object to roll and move each time to a different path ?

Discussion in 'Scripting' started by shamenraze1988, Jul 16, 2021.

  1. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    This script is attached to an object and when the object gets the force he starts rolling on the terrain.

    The object is rolling out of a box that's the Crate_0_0.
    Once the object is out of the box he starts traveling on the terrain, rotating and moving on the terrain depending on physics.

    but I want now when the object is leaving the box to face each time to another direction only on the Y and then to start moving to that direction. maybe it's impossible because the way the terrain is built so the physics make it moving to the same path each time. but maybe it's possible to make it to start moving each time to a random path on the terrain?

    I tried this in the OnCollisionExit :

    Code (csharp):
    1.  
    2. var rot = Random.rotation;
    3. transform.rotation = Quaternion.Euler(0, rot.y, 0);
    4.  
    but it does nothing much and because the object has the force and he is rolling while moving on the terrain the object rotation is changing all the time already on all the axis X Y Z so I'm to if it's possible and how to do it.

    I want the first time it's exit to give it another path or at least to start in another path not to randomize it all the time only once when it exit the box.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Roll : MonoBehaviour
    7. {
    8.     public float forceToAdd;
    9.  
    10.     private Rigidbody rb;
    11.     private bool addForce = false;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         rb = GetComponent<Rigidbody>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.        
    23.     }
    24.  
    25.     private void FixedUpdate()
    26.     {
    27.         if (addForce)
    28.         {
    29.             rb.AddForce(Vector3.right * forceToAdd * Time.deltaTime);
    30.         }
    31.     }
    32.  
    33.     private void OnCollisionEnter(Collision collision)
    34.     {
    35.         if (collision.gameObject.name == "Crate_0_0")
    36.         {
    37.             rb.drag = 0;
    38.             addForce = true;
    39.         }
    40.     }
    41.  
    42.     private void OnCollisionExit(Collision collision)
    43.     {
    44.         if (collision.gameObject.name == "Crate_0_0")
    45.         {
    46.             forceToAdd = 100;
    47.  
    48.             var rot = Random.rotation;
    49.             transform.rotation = Quaternion.Euler(0, rot.y, 0);
    50.         }
    51.     }
    52. }
    53.  
     
  2. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    I tried this :

    Code (csharp):
    1.  
    2. private void OnCollisionExit(Collision collision)
    3.     {
    4.         if (collision.gameObject.name == "Crate_0_0")
    5.         {
    6.             rb.AddForce(0,
    7.                 0,
    8.                 Random.Range(-10, 10),
    9.                 ForceMode.Impulse);
    10.  
    11.             forceToAdd = 100;
    12.         }
    13.     }
    14.  
    but two problems :

    1. It's making it random but only to the right never to the left or middle areas only to the right direction.

    2. because it's adding force randomly on the Z then the object is moving sometimes faster or slower. I don't want to change the speed of it the force but to give a random starting path.

    This is close but not working as I wanted.
     
  3. Tonafide

    Tonafide

    Joined:
    Jan 3, 2021
    Posts:
    11
    I think what you want to do is:

    Code (CSharp):
    1. private float forceModifier = 10 //I just used 10 because that is what you used in your script you can use whatever you want
    2.  
    3. private void OnCollisionExit(Collision collision)
    4.     {
    5.         if (collision.gameObject.name == "Crate_0_0")
    6.         {
    7.             var rot = Random.rotation;
    8.             rb.rotation = rot;
    9.             rb.velocity = transform.forward * forceModifier;
    10.         }
    11.     }
    this should give your object a random rotation and then apply a change in velocity to what is now forward;
     
    Last edited: Jul 17, 2021
unityunity