Hi , I am beginner in C# scripting I Got a Script from my friend and he say’s that this will help you to control the object motion in easy way. When I apply this script to a player object the script give a error saying that
"Operation “==” Cannot be assign to operands of type “string” and “Float” ???"
My player Has a rigidbody attacked and I don’t know what is the problem. Please help me I am the beginner xD
Here is My Script :-
//----------------------------------------------------------------//
using UnityEngine;
using System.Collections;
public class Easy : MonoBehaviour {
// here public means that we can access or Change the GameObject with our own object on Unity any time
// < GameObject obj > means that we can add any Object to variable because is now a GameObject
public GameObject obj;
// < Rigidbody rb > means that we can add any Rigidbody of any Object to
Rigidbody rb;
// Here We Created the Strings x,y,z,forward
private string x;
private string y;
private string z;
private string forward;
//--------------------------------//
// Use this for initialization
void Start () {
rb = obj.GetComponent ();
// Here we are giving the Values to the Strings that we created in the above
x = “x”;
y = “y”;
z = “z”;
forward = “forward”;
//------------------------------------//
}
// Update is called once per frame
void Update () {
if (Input.GetKey (“up”)) {
// Here we are calling the Go-Function that we created below
// Go is the function and means we are adding <0.5f> to the object z-axis
Go(z,0.5f);
}
if (Input.GetKey (“down”)) {
// Go is the function and means we are adding < -0.5f > to the object z-axis
Go(z,-0.5f);
}
if (Input.GetKey (“left”)) {
// Go is the function and means we are adding < -0.5f > to the object x-axis
Go(x,-0.5f);
}
if (Input.GetKey (“right”)) {
// Go is the function and means we are adding <0.5f> to the object x-axis
Go(x,0.5f);
}
}
// This function will check where to move the object along vectors or by applying Force
public void Go(string Var,float X){
// THis will add force along the player z-axis Facing At the force of X value
if (Var == forward) {
rb.AddForce(transform.forward * X);
} else
// This will move the object along x-axis At the speed of X value
if (Var == X) {
obj.transform.position += new Vector3 (X, 0f, 0f);
} else
// This will move the object along y-axis At the speed of X value
if (Var == y) {
obj.transform.position += new Vector3 (0f, X, 0f);
} else
// This will move the object along z-axis At the speed of X value
if (Var == z) {
obj.transform.position += new Vector3 (0f, 0f, X);
}
}
}
//----------------------------------------------------------------//
Thanks