I'm following a tutorial into making a game in Unity, and I've encountered a peculiar problem with my JavaScript/UnityScript:
#pragma strict
var Health = 100;
function ApplyDamage (TheDamage : int)
{
Health -= TheDamage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
Destroy (gameObject);
}
That is my script for the enemy game object, however, despite me having logic for a melee system where you have to click him twice and he dies, it doesn't work, and whenever my player character walks into the enemy it is destroyed automatically without clicks.
I'll post my melee system here too:
#pragma strict
var TheDamage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
function Update ()
{
if (Input.GetButtonDown("Fire1"))
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
(
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions. DontRequireReceiver)
);
}
}
Can anyone help me with this? It seems like a random error.
The code you posted for your melee system is missing braces {} for the first if statement, causing it to only execute var hit : RaycastHit; when the mouse is clicked, and execute the rest of the body every time Update is called.
Your code has a problem. You are not actually using the if statement correctly. This is because you didn't use {}. I will post how your code is being interpreted below:
if (Input.GetButtonDown("Fire1"))
{
var hit : RaycastHit;
}
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions. DontRequireReceiver);
}
}
Obviously that is not what you intended. Remember if you don't put the brackets then only the line directly after the if will execute when the if condition is met, the rest will execute independently of that if.
Solution
if (Input.GetButtonDown("Fire1"))
{
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
// Intentionally left the brackets out of this if to show you.
if(Distance < MaxDistance)
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions. DontRequireReceiver);
}
}
So I intentionally left out the brackets on that inside if to show you how to use it without brackets. If you are unsure, just add brackets, it will make your code cleaner and easier to understand and you will avoid bugs.
Related
I have an object called 'Player' in my scene.
I also have multiple objects called 'Trees'.
Now I'd like whenever the user clicks on a 'Tree', the 'Player' to move slowly to that position (using Lerp or moveTowards) is both O.K for me.
Now I have 2 issues with this code:
I'd like this code to be generic
Whenever I click a tree object I'd like this to move the player towards that tree. I don't want to write up this script and attach it to each tree object.
Where should I put the script?
Currently I attach this code to every tree object.
How should I write it down so that it applies to every tree object
in the scene?
If another click is made while moving, cancel previous movement and start moving to new position
How should I write it so that if I click on another object while the
player is moving towards another object that was clicked, the players stops moving towards it's previous position, and starts
moving towards the new point.
I'm having some trouble adjusting to the new UnityScript thingy. I come strictly from a Javascript background and it really seems like the 2 of them are languages with very different semantics. So if someone answers this with code(which is what I would want:) ), I'd appreciate some verbose comments as well.
I currently do this:
var playerIsMoving = false;
Public playerObject: Gameobject; //I drag in the editor the player in this public var
function update(){
var thisTreePosition = transform.point; //this store the X pos of the tree
var playerPosition = player.transform.point;
if(playerIsMoving){
player.transform.position = Vector2.MoveTowards(playerPosition, thisTreePosition, step);
}
}
function OnMouseDown(){
playerIsMoving = true;
}
I'm writing this from home where I don't have Unity and I forgot the code syntax therefore expect the above code to have typos or issues, at work it works just fine, apart from being very crude and unsophisticated.
I would recommend you to put the movement Script on the player. And how abour using Raycast to the test whether you hit a tree?
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Vector3 positionToWalkTo = new Vector3();
void Update() {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
Ray ray = new Ray(transform.position, direction);
if (Physics.Raycast(ray, out hit))
if (hit.gameObject.tag.Equals("tree")){
positionToWalkTo = hit.gameObject.transform.position;
}
}
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, positionToWalkTo, step);
}
To get something like this running, you should tag all the trees.
'JavaScript'
var target: Transform;
// Speed in units per sec.
var speed: float;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Raycasting is like shooting something into the given direction (ray) and hit is the object which got hit
if (Physics.Raycast(ray, hit)) {
if (hit.gameObject.tag = "tree")
target = hit.gameObject.transform.position; // Sets the new target position
}
}
// The step size is equal to speed times frame time.
var step = speed * Time.deltaTime;
// Move our position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
Okay, so I've done it myself (with some help from Freshchris's answer).
Here it is:
var target:Vector2;
var targetObject:GameObject;
var initialY:float;
var tolerance = 1;
var speed = 3;
var isMoving = false;
function Start(){
target = transform.position;
}
function Update () {
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); //raycast the scene
// if we hit something
if (hit.collider != null) {
isMoving=true; //it should start moving
targetObject = hit.collider.gameObject; //the target object
target = hit.collider.gameObject.transform.position;
}
}
// The step size is equal to speed times frame time.
var step = speed * Time.deltaTime;
// if it should be moving - move it - and face the player to the correct direction
if (isMoving){
if(transform.position.x>target.x){
gameObject.transform.localScale.x = -0.7;
}else {
gameObject.transform.localScale.x = 0.7;
}
transform.position = Vector2.MoveTowards(transform.position, target, step);
}
// if it reached the target object by a specified tolerance, tell it to stop moving
if(isMoving){
if((transform.position.x < target.x+tolerance)&&(transform.position.x > target.x-tolerance)){
print("position reached"); //this is fired just once since isMoving is switched to false just one line below
print(targetObject);
isMoving=false;
}
}
}
okay so my enemies are being instantiated in my main.js
but the animator is attached to this prefab of enemy ( it is a sprite animation )
it works on certian enemies
for instance
0will work
1 wont work
1,1,0,1,0,1,0,0,0,1,0,1,0 and so on it seems random
also my enemies spawn between 6 and 9 seconds.
i just cant figure this out
one problem after another.:( (guess this sums up a beginers game dev).
nearly done though.
thanks for your help stackoverflow community.
#pragma strict
var enemy : GameObject;
var speed : float = 1.0;
var enemanim : Animator;
var isdying : boolean = false;
function Start () {
this.transform.position.x = 8.325;
this.transform.position.y = -1.3;
enemanim = GetComponent(Animator);
enemanim.SetFloat("isdead",0);
}
function OnCollisionEnter2D(coll: Collision2D) {
if(coll.gameObject.CompareTag("distroy")){
Destroy(enemy.gameObject);
}
if(coll.gameObject.CompareTag("Player") && main.jumped == true){
isdying=true;
}
}
function Update () {
this.transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
this.rigidbody2D.velocity = Vector2(-5,0);
if (isdying==true){
enemanim.SetFloat("isdead",1);
}
}
You are missing
enemanim.Play();
It is working on and off because some of your Enemies are set to play automatically through the Editor.
I am developing my first game with the Unity3D engine and I have run into my first problem! Not as exciting as I thought. If I spam the jump button w my ball jumps the first time then when it lands it does not jump right away but it jumps randomly after a number of button presses. The code I am using is below.
#pragma strict
var rotationSpeed = 100;
var jumpHeight = 8;
private var isFalling = false;
function Update ()
{
//Handle ball rotation.
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
rigidbody.AddRelativeTorque (Vector3.back * rotation);
if (Input.GetKeyDown(KeyCode.W) && isFalling == false)
{
rigidbody.velocity.y = jumpHeight;
}
isFalling = true;
}
function OnCollisionStay ()
{
isFalling = false;
}
I heard this was a arithmetic behavior problem in UnityScript. I am a very beginner at programming and do not really understand the code in UnityScript and this is the first game/project I am making in Unity3D. Help would be greatly appreciated. Thanks!
You would think that something as simple as jumping should be a no-brainer, but there are a couple of gotchas here:
It can happen that Update() is called twice without any physics updates in between. This means you don't receive OnColliderEnter nor OnColliderStay in between but you still reset isFalling, blocking the jump key.
The moment the ball hits you will receive OnColliderEnter, but not OnColliderStay. This won't happen until the next physics update. If you only listen to -stay you will be one update late.
If you press jump right at the time the ball is supposed to hit the ground then your key is already down when the first collider hit registers. You will be another update late.
Objects sink a little bit into the collider. When you jump, it might take a couple of updates before you clear the collider and you will receive OnColliderStay after you jumped.
I think that last point is the biggest problem here. To solve this you need to do a couple of things:
Use both OnColliderEnter and OnColliderStay. This will imrpove your timing with one update.
Reset isFalling in FixedUpdate instead of in Update. FixedUpdate is part of the physics loop, so it is called right before OnCollisionEnter and OnCollisionStay who will set it again immediately if needed.
Remember that you are trying to jump until you are actually in the air. This allows you to clear the collider with one button press.
The code below implements these points. If you want the timing to be even tighter you must queue the next jump while you are in the air.
#pragma strict
var rotationSpeed = 100;
var jumpHeight = 8;
private var isFalling = false;
private var tryingToJump = false;
function Update ()
{
//Handle ball rotation.
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
rigidbody.AddRelativeTorque (Vector3.back * rotation);
if (Input.GetKeyDown(KeyCode.W) && !isFalling) tryingToJump = true;
if (tryingToJump)
{
if (isFalling) tryingToJump = false;
else rigidbody.velocity.y = jumpHeight;
}
}
function FixedUpdate()
{
isFalling = true;
}
function OnCollisionStay()
{
isFalling = false;
}
function OnCollisionEnter()
{
isFalling = false;
}
I'm making a game where the player touches the screen and an object instantiates . However, I only want the object to instantiate if the Raycast hits an object that is on a specific layer (or of a specific tag if that is easier). I can get the ray to cast out and instantiate a prefab where I'd like it, but when I add in the section of checking for the layer, it sends me an error (NullReferenceException:Object reference not set to an instance of an object). This seems like such a simple thing but I can't get it to work. Any help would be GREATLY appreciated!
var box : Transform;
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
if(roomController.noMore == false){
var hit : RaycastHit;
var mousePos : Vector3 = Input.mousePosition;
mousePos.z = 9;
var worldPos : Vector3 = camera.ScreenToWorldPoint(mousePos);
Debug.Log("Mouse pos: " + mousePos + " World Pos: " + worldPos + " Near Clip Plane: " + camera.nearClipPlane);
if(hit.collider.gameObject.layer == "Ground" && HierarchyType.collider != null){
clone = Instantiate(box, worldPos, Quaternion.identity);
noMore = true;
Destroy(this);
}
}
}
}
Try use Physics.Raycast.
var hit : RaycastHit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
{
if (hit.collider != null && hit.collider.gameObject.layer == LayerMask.NameToLayer("Water"))
{
Debug.Log("Instantiate");
}
}
but when I add in the section of checking for the layer, it sends me
an error (NullReferenceException:Object reference not set to an
instance of an object).
This is probably because actually you are not casting any ray and checking that an hit actually occurred( avoid the relative collider field could be null).
I suggest you to have a look at Physics.RayCast overloads, it could be significant more efficient to specify the layer (through LayerMask) while casting instead of filtering the results later.
The mechanics are set up so that when the user input ("Fire1") the distance is updated and health is deducted if the distance that the raycast found is less than or equal to 1.5.
However when I click the mouse button the distance variable is not updated thus nor is the health.
**
Melee System Code
**
#pragma strict
var Damage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
function update ()
{
if(Input.GetButtonDown("Fire1"))
{
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if(Distance < MaxDistance)
{
hit.transform.SendMessage("Apply Damage", Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
Enemy Logic Code
#pragma strict
var Health : int = 100;
function ApplyDamage(Damage : int)
{
Health -= Damage;
}
*
Please Explain All Changes.
*
Other than the solution suggested by Man of Snow (certainly correct), another error is the lower-cased "u" in the update() function. The real function that you wanna call is Update() (with the upper-cased "U"). "update()" is a function that's actually never called, not the one invoked each frame by Unity.