- Home /
How can I change vector speed?
Sorry for my English Firstly this is a cube script and it is Instantiate randomly location. cube must go randomly.if it is collide another cube or wall ,it must change its speed and direction. I did this script and it can go randomly direction but it doesnot change speed and sometimes it stop when it collide the wall.I work 2 days on this 2 problems.Please help me ! NOT:I use 5.3.1p1. Wall and cube has box collider.Thank you for your answer. using UnityEngine; using System.Collections; using UnityEngine.UI;
public class CanScript : MonoBehaviour {
public Rigidbody rb;
public Vector3 git;
public float speed=5f;
void Awake()
{
rb = GetComponent<Rigidbody>();
}
void Start()
{
git.x = Random.Range(-40, 40);
git.y = 0f;
git.z = Random.Range(-40, 40);
}
void FixedUpdate()
{
rb.velocity = new Vector3(git.x, 0, git.z); // i cant change speed.i want to use it
}
void OnCollisionEnter (Collision col) //
{
if (col.gameObject.tag == "objetag")
{
speed = Random.Range(5, 40); // i want to use this speed
git.x = Random.Range(-40, 40);
git.y = 0f;
git.z = Random.Range(-40, 40);
}
}
}
Your answer
Follow this Question
Related Questions
Can't access the velocity of a rigidbody: 'Rigidbody' does not contain a definition for 'velocity' 1 Answer
How would I counteract the force of the velocity and set it to zero after the input has stopped? 3 Answers
Need help for my Arkanoid-like game. i got problem to make my ball reflects on the wall 1 Answer
Incorrect velocity after collision 1 Answer