Rigibody Velocity Wont Work?
Hello! I want my boss enemy to spawn knifes, that fly in the air and fall to the ground. The thing is, that nothing I do to add force, my knifes still just drop to the floor. They get instantiated once a timer runs out. Here is the Boss script...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossScript : MonoBehaviour
{
//Chef Zombie Attributes
public GameObject chefZombie;
public GameObject throwingKnifes;
public Transform throwingKnifesSpawn;
private int rand;
public float timer;
public bool initiateBossFight;
//Puh, wo fängt man an...
//Als erstes muss der Chef Zombie aktiv werden, sobald der Spieler den Raum betritt.
// Vielleicht eher mit Bool arbeiten?
//Ein Collider hier zu machen bringt nichts...
// HAHA! Ich weiß nun, wie man referenziert! Der Trigger besitzt eine Referenz zu diesem Skript, und ändert dabei den Bool, und löscht gleichsam den Trigger, damit der nicht nochmal ausgelöst wird
void Start()
{
initiateBossFight = false; // Boss ist inaktiv...
}
private void Update()
{
if (initiateBossFight == false)
{
chefZombie.SetActive(false);
}
if(initiateBossFight == true)
{
BossFight();
}
}
void BossFight()
{
chefZombie.SetActive(true);
rand = Random.Range(3, 10);
if(timer <= 0)
{
Instantiate(throwingKnifes, throwingKnifesSpawn.position, throwingKnifesSpawn.rotation);
timer = rand;
}
else
{
timer -= Time.deltaTime; //der Timer muss ja runter laufen...
}
}
}
And here is the script on the knife prefab...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThrowingKnifes : MonoBehaviour
{
public Rigidbody2D throwingKnifesRB;
public float throwingKnifesSpeed = 50.000f;
void Start()
{
throwingKnifesRB.AddForce(transform.forward * 50.000f);
}
// Update is called once per frame
void Update()
{
}
}
Can somebody please explain to me, why the knifes just drop to the ground? Thanks!
Answer by xxmariofer · Sep 03, 2020 at 07:12 AM
hello change your code to this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThrowingKnifes : MonoBehaviour
{
public Rigidbody2D throwingKnifesRB;
public float throwingKnifesSpeed = 50.000f;
void Start()
{
throwingKnifesRB = GetComponent<RigidBody>();
throwingKnifesRB.AddForce(transform.forward * 50.000f);
}
// Update is called once per frame
void Update()
{
}
}
Thank you for your help, but it still wont work... I read somehwere that force doesnt work with time.deltatime, could be that the problem?
any errors show in theconsole? the code is fine, and no time.deltatime is fine
private Rigidbody2D throwingKnifesRB;
public float throwingKnifesSpeed;
void Start()
{
throwingKnifesRB = GetComponent<Rigidbody2D>();
throwingKnifesRB.AddForce(transform.up * throwingKnifesSpeed);
}
// Update is called once per frame
void Update()
{
}
Got it now thanks!
Your answer
Follow this Question
Related Questions
Shooting a bullet that stops in place when mouse is released 0 Answers
RiigidBody2D.AddForce() isnt working at all. 0 Answers
Rigidbody Addforce not working as expected ( video included) 0 Answers
Addforce for 2d game is not doing anything. 0 Answers
Trying to get objects to instantiate and move at different angles 1 Answer