step 10 space shooter asteroid random rotation not working
I have followed this script but it still won't rotate
using UnityEngine;
using System.Collections;
public class RandomRotator : MonoBehaviour
{
public float tumble;
void Start ()
{
rigidbody.angularVelocity = Random.insideUnitSphere * tumble;
}
}
I've also changed it to "GetComponent" code because I'm using the new 5.0 script. but still doesn't work.
Answer by DoTA_KAMIKADzE · Apr 19, 2015 at 01:10 AM
GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * tumble;
Also make sure your tumble value is > 0.
yes, done that, still not rotating.
well, I searched and tried a few alternative and this one works pretty well I think. Though I still don't understand why the code on the training won't work with me.
private float maxRotationSpeed;
private Vector3 rotSpeed;
void Start()
{
maxRotationSpeed = 50;
rotSpeed = Random.insideUnitSphere * maxRotationSpeed;
}
void Update()
{
GetComponent<Transform> ().Rotate (rotSpeed * Time.deltaTime);
}
Well there are 2 possible problems then:
1)You forgot to add Rigidbody component to your asteroid which holds your RandomRotator.
2)You forgot to set your tumble value to something else than 0. Either via code or via inspector (because you use public I think you set it in inspector).
Or both of the above.
Answer by AuggoDoggo · Jul 08, 2015 at 06:18 AM
I had the same problem, double checked everything, followed every suggestion in this thread and then I started fooling around with the Random.insideUnitSphere and replaced it by creating a vector3 with all of its values being "Random.value". Nothing worked... except for capitalizing my Start function...
void Start()
NOT
void start()
I feel dumb and defeated sometimes but then I realize I learned another simple lesson today that I won't be needing again tomorrow...
I'm sorry if this is not the answer to OPs problem, but perhaps it will help someone with my problem.
I had the same problem, Ctrl through the forums to find this..... Thanks!
This actually solved my problem without me having to look far, thanks!
Answer by brandoncomputer · May 23, 2015 at 02:26 PM
using UnityEngine; using System.Collections;
public class RandomRotator : MonoBehaviour {
public float tumble;
void Start()
{
GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * tumble;
}
}
Answer by $$anonymous$$ · Oct 23, 2015 at 05:04 AM
Simple fix trick. Check Kinematic on the asteroid rigid body. Save scene then project. close unity. reopen unity and uncheck Kinematic. Make sure the code looks like this.
using UnityEngine;
using System.Collections;
public class RandomRotator : MonoBehaviour
{
public float tumble;
Also make sure to go ahead and tag the asteroid as "hazard." private Rigidbody rb;
void Start ()
{
rb = GetComponent <Rigidbody> ();
rb.angularVelocity = Random.insideUnitSphere * tumble;
}
}
The above fix works but the asteroid position is also changing in all the direction, how can i fix it?