- Home /
Adding Forces. Making It Move Strangely.
I have a 2D top do`enter code here`wn shooter with XZ Axis. It's basic, move around and shoot with mouse, but when you click away and or on the character the bullets move very weird and never constant...
When you click near the character the bullets move left or right and never straight to their relative rotation. When you click farther away from the character they travel a lot faster than the usual speed.
Here's an example in a webplayer. Click on the character and farther away from it and you'll see how the bullet travels. Im guessing it may have to do something with the AddForce.
using UnityEngine;
using System.Collections;
public class playerShoot : MonoBehaviour {
public Transform bullet;
public float bulletSpeed;
public float sprayFactor;
public float minLength;
public float maxLength;
public float shotTimer;
public bool canShoot = true;
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(0)){
if(canShoot){
var sprayY = Random.Range(-sprayFactor,sprayFactor);
var bulletLength = Random.Range(minLength,maxLength);
var activeBullet = Instantiate(bullet,transform.position,transform.rotation) as Transform;
activeBullet.transform.localScale = new Vector3(.2f,bulletLength,.2f);
activeBullet.transform.Rotate(new Vector3(0f,sprayY,0f));
activeBullet.rigidbody.AddRelativeForce(Vector3.forward * bulletSpeed);
canShoot=false;
StartCoroutine("shotTime");
}
else{
}
}
}
IEnumerator shotTime(){
yield return new WaitForSeconds(shotTimer);
canShoot=true;
}
}
Is your main character using a "lookAt" command to look towards the mouse? I'm wondering if his transform is actually tilting up or down as the mouse gets closer to him.
maybe you can try this to replace AddForce
activeBullet.rigidbody.velocity = transform.TransformDirection(new Vector3(0,0,bulletSpeed));
@$$anonymous$$antBiscuits $$anonymous$$y player is using a LookRotation
Vector3 to = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distanceFromCamera));
Quaternion rot = Quaternion.LookRotation(to - transform.position, Vector3.up);
transform.rotation =rot;
@Prototype7 That actually fixed the odd movement when you click on the character, but the firing speed still isn't constant when you click away or closer to the character.
For me it easier to make empty game object and put this as a child of your player(turret) this will make bullet direction follow your player, and add this script to the empty. you also want to avoid collusion between your bullet by setting collider to is trigger.
public class Launcher : $$anonymous$$onoBehaviour
{
public Rigidbody bullet;
public float bulletSpeed;
// Update is called once per frame
void Update()
{
if (Input.Get$$anonymous$$ouseButton(0))
{
var activeBullet = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
activeBullet.rigidbody.velocity = transform.TransformDirection(new Vector3(0, 0, bulletSpeed));
Physics.IgnoreCollision(transform.root.collider, activeBullet.collider, true);
}
}
}
Your answer
Follow this Question
Related Questions
Imported model is rotated 90 degrees off of straight, can't use any transforms 1 Answer
Relative force 1 Answer
relativeVelocity how to? (noob question) 2 Answers
AddForce Relative to Rotation 1 Answer
Add Relative Force 2 Answers