Question by
kenonunity · Feb 03, 2019 at 03:02 PM ·
scripting problemraycastnewbie
RayCast issues
Hi, i hope that someone can help me with this. im trying to make a simple shooter game (yes i am new to this) and i have got it to fire a new item forward on each click. but would like it to fire towards where i click on the screen i have a ray cast in place but the line just goes up and off screen and the items still just fire forward in one place.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooter : MonoBehaviour
{
public Rigidbody projectile;
public Transform shotPos;
public float shotForce = 100f;
public float moveSpeed = 10f;
public float currentax = 0;
public float totalax = 10;
public Rigidbody Instantiate(Rigidbody projectile, Vector3 position, Quaternion rotation, Quaternion identity)
{
throw new NotImplementedException();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{ // only do anything when the button is pressed:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.DrawLine(shotPos.position, hit.point *10);
Rigidbody shot = Instantiate(projectile, GameObject.Find("shotPos").transform.position, Quaternion.identity);
shot.AddForce((hit.point - shotPos.transform.position) * shotForce);
}
}
}
}
can anyone help me see what i clearly cant, and where its going wrong
thanks
Comment
Your answer
Follow this Question
Related Questions
Raycast not taking full length 0 Answers
Scripting Errors 1 Answer
Raycast not detected on instantiated object 0 Answers
I'm trying to use a ray to grab objects but, my script isn't working? 0 Answers
Raycast Enemy AI shooting script 1 Answer