- Home /
Rigidbody speed in some senes different?!
Hello out there, the Controller of a Enemy-bullet is speeding the bullet up:
void Update ()
{
rb.velocity = transform.forward * speed;
}
In some scenes, the bullet travels at the correct speed, in others it is very very slow, but still a bit moving.
here the complete script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyBullet : MonoBehaviour {
[HideInInspector]
public float speed;
[HideInInspector]
public float makePlayerSlower;
[HideInInspector]
public float lifeTime;
[HideInInspector]
public ParticleSystem explosion;
float lifeTimer;
GameObject player;
Rigidbody rb;
bool mayDestroy = true;
bool wasStuck;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update ()
{
rb.velocity = transform.forward * speed;
wasStuck = false;
lifeTimer += Time.deltaTime;
if ((lifeTimer >= lifeTime) && mayDestroy)
Destroy(gameObject);
}
void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
transform.parent = other.transform;
rb.isKinematic = true;
player = other.gameObject;
player.GetComponent<PlayerControl>().speedReduction = makePlayerSlower;
player.GetComponent<PlayerControl>().AddStickyBall(gameObject);
GetComponent<Collider>().enabled = false;
mayDestroy = false;
wasStuck = true;
}
else if(!other.CompareTag("Enemy"))
{
Destroy(gameObject);
}
}
void OnDestroy()
{
ParticleSystem explosionClone = Instantiate(explosion, transform.position, transform.rotation);
Destroy(explosionClone, 1f);
if(wasStuck)
player.GetComponent<PlayerControl>().RemoveStickyBall();
}
}
I am pretty shure, that i don't change the transform of the bullet in any other scripts! And i have now Idea...
(btw: if you have any advice for my in my way of scripting, please let me know! :D)
Thanks for reading and trying to help! WbrJr
Answer by admiralnoobdog · Dec 05, 2017 at 08:06 PM
Try using Time.deltaTime
void Update ()
{
rb.velocity = transform.forward * speed*Time.deltaTime;
}
and try using FixedUpdate() instead of Update() when you are trying to move objects using RigidBody
`
Ok, wow...I am Pretty stupid. I never really payed attantion to it, but Thanks alot!! :D
First time running the game with the change, it worked. But after tweaking and trying a secound time, it was as slow as always... any idea? :/ thanks
Your answer
Follow this Question
Related Questions
Comparing rigidbody speeds 1 Answer
rigidbody It's going up and bounces when is moving 1 Answer
Add velocity relative to ground? 1 Answer