Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPaul Yang
Courses Plus Student 1,112 PointsHow to rotate my weapon into another direction by changing this script?
Hi Im currently trying to make this script work with a prefab. the prefab itself goes to the right direction on its own. but as soon as i add this prefab under this script, the prefab goes other way around. please help me identify an alternative with this script so i can use it for many circumstances
using UnityEngine;
public class WeaponScript : MonoBehaviour { //Projectile prefab for shotting public Transform shotPrefab;
// cooldown in the seconds between two shots;
public float shootingRate = 0.25f;
// cooldown
private float shootCooldown;
void Start ()
{
shootCooldown = 0f;
}
void Update ()
{
if (shootCooldown > 0)
{shootCooldown -= Time.deltaTime;
}
}
// Create a new projectile if possible
public void Attack(bool isEnemy)
{
if (CanAttack)
{
shootCooldown = shootingRate;
// Create a new shot
var shotTransform = Instantiate(shotPrefab) as Transform;
// Assign position
shotTransform.position = transform.position;
// The is enemy property
ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript>();
if (shotPrefab != null)
{
shot.isEnemyShot = isEnemy;
}
// Make the weapon shot always towards is
MoveScript move = shotTransform.gameObject.GetComponent<MoveScript>();
if (move != null)
{
move.direction = this.transform.right; // towarrds in 2D space is the right of the sprite
}
}
}
// is the weapon ready to create a new projectile?
public bool CanAttack
{
get
{
return shootCooldown <= 0f;
}
}
}