Hello,
I'm trying to make a game. but i'm stuck now for days, maybe it's because i'm using C# and JS.
But what i want to happen is: when the player hold the arrow down and an object collide with the player the object is going to play an animation ones and then dissapears.
the C# code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 15f;
public float JumpPower = 350f;
public bool grounded;
public bool magspringen = false;
public GameObject otherObject;
Animator otherAnimator;
private Rigidbody2D rb2d;
private Animator anim;
void Start()
{
rb2d = gameObject.GetComponent();
anim = gameObject.GetComponent();
otherAnimator = otherObject.GetComponent();
}
void Update()
{
anim.SetBool("Grounded", grounded);
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.DownArrow))
{
if (grounded)
{
anim.Play("walk");
transform.position += Vector3.right * speed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, 0);
}
else
{
transform.position += Vector3.right * speed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, 0);
}
}
if (Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.DownArrow))
{
if (grounded)
{
anim.Play("walk");
transform.position += Vector3.left * speed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, 180);
}
else
{
transform.position += Vector3.left * speed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, 180);
}
}
if (Input.GetKeyDown(KeyCode.UpArrow) && grounded && !Input.GetKey(KeyCode.DownArrow) && magspringen == true)
{
anim.Play("jump");
rb2d.AddForce(Vector3.up * JumpPower);
}
if (Input.GetKey(KeyCode.DownArrow) && grounded)
{
anim.Play("buk");
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Respawn")
{
magspringen = true;
}
if(other.gameObject.tag == "antagonist")
{
otherAnimator.Play("gat");
}
}
}
and the JS code:
#pragma strict
public var score = 0;
public var antagonist : GameObject;
public var anta1 :GameObject;
public var anta2 :GameObject;
public var donker: GameObject;
function OnTriggerEnter2D (Col: Collider2D)
{
if(Col.CompareTag("Antagonist"))
{
if (Input.GetKey(KeyCode.DownArrow)) {
score=score +1;
}else{
Destory(antagonist)
}
}
if(Col.CompareTag("anta1"))
{
if (Input.GetKey(KeyCode.DownArrow)) {
score=score +1;
}else{
Destroy(anta1);
}
}
if(Col.CompareTag("anta2"))
{
if (Input.GetKey(KeyCode.DownArrow)) {
score=score +1;
}else{
Destroy(anta2);
}
}
{
if(Col.CompareTag("Respawn") && score <= 2)
{
Destroy(donker);
}
}
}
So what it is doing now is, if the player touches a the object when holding arrow down it gets a score but doesn't play the animation. I tried doing it in the C# code with otherAnimator.
↧