So what this should do is when you press “q” (which is the FirstSkill), it creates a cube on the top right part of the player (the player is a capsule) and move it to a position in front of the player, pretty much throwing it.
using System.Collections;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
private float speed = 15f;
private float jumpForce = 15f;
private float gravity = 30f;
private Vector3 moves = Vector3.zero;
private Vector3 miniMeteorDestination = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z + 15);
private float miniMeteorSpeed = 30f;
void Start () {
Cursor.lockState = CursorLockMode.Locked;
}
void Update () {
Stamina staminaScript = transform.GetComponent<Stamina>();
CharacterController controller = GetComponent<CharacterController>();
float translation = Input.GetAxis("Vertical")*speed;
float straffe = Input.GetAxis("Horizontal")*speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(straffe, 0, translation);
if(Input.GetKeyDown("escape")){
Cursor.lockState = CursorLockMode.None;
}
if(Input.GetButtonDown("Jump") && controller.isGrounded){
moves.y = jumpForce;
}
moves.y -=gravity*Time.deltaTime;
controller.Move(moves *Time.deltaTime);
if(Input.GetButtonDown("Teleport") && staminaScript.stamina > 199){
transform.Translate(0, 0, 8);
staminaScript.stamina -= 200;
}
if(Input.GetButtonDown("Teleport") && staminaScript.stamina < 200){
transform.Translate(0, 0, 0);
}
if(Input.GetButtonDown("FirstSkill") && staminaScript.stamina > 99){
staminaScript.stamina -= 100;
MiniMeteorFunction();
}
}
IEnumerator MiniMeteorFunction(){
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = "MiniMeteor";
cube.transform.position = new Vector3(transform.position.x + 1f, transform.position.y + 1.5f, transform.position.z);
yield return new WaitForSeconds(0.5f);
cube.transform.position = Vector3.MoveTowards(cube.transform.position, miniMeteorDestination, miniMeteorSpeed * Time.deltaTime);
}
}
The errors are:
Assets/Scripts/PlayerMovement.cs(11,54): error CS0236: A field initializer cannot reference the nonstatic field, method, or property UnityEngine.Component.transform' Assets/Scripts/PlayerMovement.cs(11,76): error CS0236: A field initializer cannot reference the nonstatic field, method, or property
UnityEngine.Component.transform’
Assets/Scripts/PlayerMovement.cs(11,102): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform’
Thanks in advance.