- Home /
Throwing a rock, and making it cause damage
At a point in the game, there are stones littered across a road. The player has to chase an enemy and pick stones up to throw at the enemy before they escape to the end of the road.
I would help to make a script that automatically picks up the stone as soon as the player gets near it, and then right-click to throw the stone. After the stone gets thrown they can pick up the same stone or pick up a different stone. The player will automatically ignore any other stones they walk over so they can't have two or three stones at once. The stone has to interact with the enemy, as in it bounces off of him, instead of just going straight through.
I'm really not looking for a "charge-up" mechanic, like the longer you hold a button it'll get sent farther. I'd much prefer to build something a script that constantly sends it "x" distance away The game perspective is in third person so, instead of it being infront of the camera, i'd like for it to be in front of the actual player. But, I don't have any animation for picking it up, so I would like to know how to make it so it just hovers a bit in front of the player
I have tried finding scripts online and through youtube tutorials but a lot of them are outdated, or just plain do not work. I already have the player, scene, and stone models set up. Below is my health script (which is attached to a very basic health bar)
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Health : MonoBehaviour
{
public Image currentHealthbar;
public Text ratioText;
private float hitpoint = 100;
private float maxHitpoint = 100;
private void Start()
{
UpdateHealthbar();
}
private void UpdateHealthbar()
{
float ratio = hitpoint / maxHitpoint;
currentHealthbar.rectTransform.localScale = new Vector3(ratio, 1, 1);
ratioText.text = (ratio * 100).ToString("0") + '%';
}
private void TakeDamage(float damage)
{
hitpoint -= damage;
if (hitpoint < 0)
{
hitpoint = 0;
Debug.Log("Dead!");
}
UpdateHealthbar();
}
}
I'm still very new to Unity so when I get an idea for a game mechanic or anyhting like that, I find it hard to actually put it down into C#. I just need someone to help me start it off, and answer my questions as I trail and error it.
Correct me if I'm wrong, but from the way you've worded your question, it sounds as if you're asking the Unity Answers community to just code everything for you. That's not ok here (try the forums for stuff like that), but if you worded the question a bit differently you could probably get a few good pointers for where to start coding this yourself. :)
All done, @Eoku_ ! Sorry if I made it seem like I was demanding someone to build a script for me. I really just want some help starting out, and a fast replier to help me with any questions I have!
Yeah, it's no problem. :) Your question is understandable an well organized, so it's good otherwise (I feel like I may have come off a bit harsh, sorry about that). I wish you the best of luck finding help!
Answer by TanselAltinel · Oct 30, 2017 at 07:54 AM
I'm pretty sure this is the tutorial you are looking for: https://unity3d.com/learn/tutorials/topics/scripting/intro-shooter-mechanics
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Raycast problem 1 Answer
Diagonal Movement Question 1 Answer