i am currently working on the controls for my game. they will be similar to robotron,smash tv, or geometry. i have been trying to figure out for 2 days now how to instantiate my projectile prefab at the correct rotation and angle to match my right thumbstick input.
The prefab i am trying to spawn is placed here Assets/Prefabs/Projectile.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
Vector3 vNewInput = new Vector3(Input.GetAxis("Right Horizontal"), Input.GetAxis("Right Vertical"), 0.0f);
if (vNewInput.sqrMagnitude < 0.1f)
{
return;
}
else
{
var direction = new Vector3 (Input.GetAxis ("Right Horizontal"), Input.GetAxis ("Right Vertical"), 0.0f);
var rotation = Quaternion.LookRotation (direction, Vector3.up);
transform.rotation = rotation;
Instantiate (Projectile, direction, rotation);
Here’s the full code and errors I’m getting
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public Rigidbody projectile;
public float speed = 0.2f;
Animator animator;
int dirInt;
Vector3 v3;
Vector3 shoot;
// Use this for initialization
void Start () {
animator = this.GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
float vertical = Input.GetAxis ("Vertical");
float horizontal = Input.GetAxis ("Horizontal");
//This moves our player
v3 = new Vector3(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical"),0);
transform.Translate(speed * v3.normalized);
//This Lets our player shoot
Vector3 shootangle = new Vector3(Input.GetAxis("Right Horizontal"), Input.GetAxis("Right Vertical"), 0f);
//Only do work if meaningful
if (shootangle.sqrMagnitude < 0.1f)
{
return;
}
else
{
var direction = new Vector3 (Input.GetAxis ("Right Horizontal"), Input.GetAxis ("Right Vertical"), 0.0f);
var rotation = Quaternion.LookRotation (direction, Vector3.up);
transform.rotation = rotation;
Instantiate (Projectile , direction, rotation);
}
//This controls the direction we face and which animation plays
if (horizontal > 0) {
animator.SetInteger ("Direction", 1);
dirInt = 1;
} else if (horizontal < 0) {
animator.SetInteger ("Direction", 3);
dirInt = 3;
} else if (vertical > 0) {
animator.SetInteger ("Direction", 2);
dirInt = 2;
} else if (vertical < 0) {
animator.SetInteger ("Direction", 0);
dirInt = 0;
} else if (horizontal > 0 && vertical > 0) {
animator.SetInteger ("Direction", 1);
dirInt = 1;
}
else if(horizontal == 0)
{
if(dirInt == 1)
{
animator.SetInteger("Direction",5);
}
if(dirInt == 2)
{
animator.SetInteger("Direction",6);
}
if(dirInt == 3)
{
animator.SetInteger("Direction",7);
}
if(dirInt == 0)
{
animator.SetInteger("Direction",4);
}
}
}
}
Assets/Scripts/Player.cs(52,62): error CS0119: Expression denotes a type', where a
variable’, value' or
method group’ was expected
Assets/Scripts/Player.cs(52,49): error CS1502: The best overloaded method match for `UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)’ has some invalid arguments
Assets/Scripts/Player.cs(52,49): error CS1503: Argument #1' cannot convert
object’ expression to type `UnityEngine.Object’
you have declared projectile on Line 6
public Rigidbody projectile;
however on line 52 you are instantiating Projectile prefab.
Instantiate (Projectile , direction, rotation);
as revolute says C# is case sensitive.
ensure that all entries to your projectile are of the same case. amment line 52 to read…
Instantiate (projectile , direction, rotation);
See how that goes