I wrote a script to make a raycast from the player and if it hits a weapon that I have on the wall I can press q and it will deduct one Level Point and make my damage go up. But I can’t seem to get it to work.
function Update(){
if (Input.GetButtonDown("Fire1")){
Shoot();
}
if(Input.GetKeyUp("q"))
{
print("You pressed q");
if(Physics.Raycast(transform.position,transform.forward,hit,5))
{
print("The Raycast is working");
if (hit.collider.gameObject.CompareTag("Weapon Upgrade"))
{
print("It knows that the weapon is a Weapon Upgrade");
if (Score.levelPoints > 0)
{
print("It is applying the upgrade.");
Score.levelPoints -=1;
playerDamage += 50;
}
}
}
}
It prints you pressed q and the raycast is working but it doesn’t print any farther than that even when I am looking directly at the gun and am really close to it. I would also like to be able to print out a message on the screen to tell the player that they can press q to upgrade it.
Does your gun have a collider? Insert this just below the print() statement: Debug.DrawRay(transform.position, transform.forward * 5.0); ...and see if the ray is really hitting the object.
– robertbuAlso debug what you hit: print("The Raycast is working, and hit: " + hit.collider.gameObject);
– BasteYes. Well it has a rigid body. When I put that code under my print() statement it doesn't make a line...
– MmrwIt has to have a collider too, not only a rigidbody. Check for that. Check also that it does have the tag assigned in the inspector.
– dmg0600