I try to build character movement system in unity, with the the following c# code:
private void moveplayer()
{
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
RB.AddForce(Forward);
Debug.Log("Moving Forward");
}
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
RB.AddForce(Left);
Quaternion rotX1 = Quaternion.Euler(0, -5, 0);
RB.MoveRotation(rotX1 * player.rotation);
Debug.Log("Moving Left");
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
RB.AddForce(Right);
Quaternion rotX1 = Quaternion.Euler(0, 5, 0);
RB.MoveRotation(rotX1 * player.rotation);
Debug.Log("Moving Right");
}
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
RB.AddForce(Back);
Debug.Log("Moving Backward");
}
if (Input.GetKey(KeyCode.Space))
{
RB.AddForce(Up);
}
if (Input.GetKey(KeyCode.UpArrow) == false || Input.GetKey(KeyCode.W) == false || Input.GetKey(KeyCode.DownArrow) == false || Input.GetKey(KeyCode.S) == false || Input.GetKey(KeyCode.LeftArrow) == false || Input.GetKey(KeyCode.A) == false || Input.GetKey(KeyCode.RightArrow) == false || Input.GetKey(KeyCode.D) == false)
{
RB.angularVelocity = Vector3.zero;
}
the player is moving and rotating, but I wish to limit the y rotation between -90 to 90 degrees so that if the player.rotation.y reaches -90, the player could only rotate right, and when it reaches 90 he could rotate only left.
how can I do it?
I tried all sort of conditions such as:
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) {
if (player.rotation.y <= -90) {
RB.AddForce(Left);
Quaternion rotX1 = Quaternion.Euler(0, -5, 0);
RB.MoveRotation(rotX1 * player.rotation);
Debug.Log("Moving Left");
} else {
Quaternion rotX1 = Quaternion.Euler(0, 0, 0);
RB.MoveRotation(rotX1 * player.rotation);
}
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) {
if (player.rotation.y >= 90) {
RB.AddForce(Right);
Quaternion rotX1 = Quaternion.Euler(0, 5, 0);
RB.MoveRotation(rotX1 * player.rotation);
Debug.Log("Moving Right");
} else {
Quaternion rotX1 = Quaternion.Euler(0, 0, 0);
RB.MoveRotation(rotX1 * player.rotation);
}
}
but it doesn't seams to work
Related
Having a bit of a problem in Unity3D. I have a fly camera with both a box collider and rigidbody, and it still moves through my terrain, which has a terrain collider and a rigidbody. The thing is, it interacts with my capsules just fine, bounces them away and everything. Obvious this is the exact opposite of what I want ha ha...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyCamera : MonoBehaviour
{.
/*
Writen by Windexglow 11-13-10. Use it, edit it, steal it I don't care.
Converted to C# 27-02-13 - no credit wanted.
Simple flycam I made, since I couldn't find any others made public.
Made simple to use (drag and drop, done) for regular keyboard layout
wasd : basic movement
shift : Makes camera accelerate
space : Moves camera on X and Z axis only. So camera doesn't gain any height*/
float mainSpeed = 25.0f; //regular speed
float shiftAdd = 250.0f; //multiplied by how long shift is held. Basically running
float maxShift = 1000.0f; //Maximum speed when holdin gshift
float camSens = 0.25f; //How sensitive it with mouse
private Vector3 lastMouse = new Vector3(255, 255, 255); //kind of in the middle of the screen, rather than at the top (play)
private float totalRun = 1.0f;
void Update()
{
if (Input.GetKey(KeyCode.R))
{
transform.position = new Vector3(26f, 4f, 14f);
}
lastMouse = Input.mousePosition - lastMouse;
lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0);
lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x, transform.eulerAngles.y + lastMouse.y, 0);
transform.eulerAngles = lastMouse;
lastMouse = Input.mousePosition;
//Mouse camera angle done.
//Keyboard commands
float f = 0.0f;
Vector3 p = GetBaseInput();
if (Input.GetKey(KeyCode.LeftShift))
{
totalRun += Time.deltaTime;
p = p * totalRun * shiftAdd;
p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
}
else
{
totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
p = p * mainSpeed;
}
p = p * Time.deltaTime;
Vector3 newPosition = transform.position;
if (Input.GetKey(KeyCode.Space))
{ //If player wants to move on X and Z axis only
transform.Translate(p);
newPosition.x = transform.position.x;
newPosition.z = transform.position.z;
transform.position = newPosition;
}
else
{
transform.Translate(p);
}
}
private Vector3 GetBaseInput()
{ //returns the basic values, if it's 0 than it's not active.
Vector3 p_Velocity = new Vector3();
if (Input.GetKey(KeyCode.W))
{
if (transform.position.x > 2 && transform.position.x < 53)
{
if (transform.position.y > 0 && transform.position.y < 40)
{
if (transform.position.z > 5 && transform.position.z < 52)
{
p_Velocity += new Vector3(0, 0, 1);
}
}
}
}
if (Input.GetKey(KeyCode.S))
{
if (transform.position.x > 2 && transform.position.x < 53)
{
if (transform.position.y > 0 && transform.position.y < 40)
{
if (transform.position.z > 5 && transform.position.z < 52)
{
p_Velocity += new Vector3(0, 0, -1);
}
}
}
}
if (Input.GetKey(KeyCode.A))
{
if (transform.position.x > 2 && transform.position.x < 53)
{
if (transform.position.y > 0 && transform.position.y < 40)
{
if (transform.position.z > 5 && transform.position.z < 52)
{
p_Velocity += new Vector3(-1, 0, 0);
}
}
}
}
if (Input.GetKey(KeyCode.D))
{
if (transform.position.x > 2 && transform.position.x < 53)
{
if (transform.position.y > 0 && transform.position.y < 40)
{
if (transform.position.z > 5 && transform.position.z < 52)
{
p_Velocity += new Vector3(1, 0, 0);
}
}
}
}
return p_Velocity;
}
}
This flycam isn't mine so if it's not optimal for the use I'm looking for, please notify me about that too.
Basically you are. Transforming it not adding velocity thats why it is crossing the collider
You need to get reference of the rigidbody then add velocity to it
Then it won't cross the collider
You need to modify your code for that
Where you are transforming
Instead of transforming
You need to
Rigidbody.velocity= vector3(.....)
Never mind, I fixed the problem.
I ended up having Kinematic ticked on the box collider of the camera, so that must have messed it up.
I'm trying to make my character idle, run, jump and fall. I did it, but i have bug with runing animation. While he goes left or right, he doesn't play animation "run", which called by integer "noob" with parameter "2",he plays animation "fall", which "noob" is parameter "4"
I'm using Unity 4.5.x and C# code
using UnityEngine;
using System.Collections;
public class movement : MonoBehaviour {
Rigidbody2D rb;
Animator anim;
public int jumpCount = 0;
public Camera main;
float jumpTimer = 0;
void Start () {
rb = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator>();
}
void Update () {
if (Input.GetKeyDown (KeyCode.X) && jumpCount < 2 && jumpTimer == 0) {
jump ();
jumpCount += 1;
jumpTimer = 1;
StartCoroutine("jumpTime");
}
if (rb.velocity.y > 0) {
Flip();
anim.SetInteger ("noob", 3);
}
if (rb.velocity.y < 0) {
Flip();
anim.SetInteger ("noob", 4);
}
if (Input.GetAxis ("Horizontal") == 0 && rb.velocity.y == 0) {
anim.SetInteger ("noob", 1);
}
if (rb.velocity.y == 0 && Input.GetAxis ("Horizontal") != 0) {
Flip();
anim.SetInteger("noob", 2);
}
if (rb.velocity.y == 0) {
jumpCount = 0;
}
}
void FixedUpdate(){
rb.velocity = new Vector2 (Input.GetAxis ("Horizontal") * 12f, rb.velocity.y);
}
void jump(){
rb.AddForce (transform.up * 14F, ForceMode2D.Impulse);
}
void Flip() {
if (Input.GetAxis ("Horizontal") > 0) {
transform.localRotation = Quaternion.Euler (0, 0, 0);
}
if (Input.GetAxis ("Horizontal") < 0) {
transform.localRotation = Quaternion.Euler (0, 180, 0);
}
}
IEnumerator jumpTime() {
yield return new WaitForSeconds (0.4f);
jumpTimer = 0;
}
}
I tried to do something with jumCount, jumpTimer and velocity.y, but nothing helped
I would suggested making a grounded and jumping bool, you can use this bool to determine whether your character is airbourne. Also using else if conditional statements may help here to avoid setting your value more than once. Then you can do something like this:
if (grounded && jumping) {
Flip();
anim.SetInteger ("noob", 3);
jumping = false;
}
else if (!grounded) {
Flip();
anim.SetInteger ("noob", 4);
}
else if (Input.GetAxis ("Horizontal") == 0 && grounded) {
anim.SetInteger ("noob", 1);
}
else if (grounded && Input.GetAxis ("Horizontal") != 0) {
Flip();
anim.SetInteger("noob", 2);
}
I'm trying to achieve firing from a 2D Player, attached the "spawn point" and made it a child of the Player, when I try to flip the sprite it works, but it does not flip the "spawn point" so when I turn left, my "spawn point" is still looking right, so when I shoot, the bullets go to the left, not to the right.
Full script:
public class PlayerController : MonoBehaviour
{
void FixedUpdate()
{
RaycastHit2D hit = Physics2D.BoxCast(this.transform.position, new Vector2(0.4f, 0.1f), 0f, Vector2.down, groundCheckRadius, groundMask); //using this for a bigger and more accurate ground check
isTouchingGround = (hit.collider != null) ? true : false;
movementInput = Input.GetAxis("Horizontal");
CheckIfStuck(); //Checks if Mario is trying to walk into the wall and get stuck
if (!isDead)
{
if ((playerRigidbody2D.velocity.x > 0 && !isFacingRight) || (playerRigidbody2D.velocity.x < 0 && isFacingRight))
{
playerAnimator.SetBool("turning", true);
}
else
{
playerAnimator.SetBool("turning", false);
}
float movementForceMultiplier = Mathf.Max(maxHorizontalSpeed - Mathf.Abs(playerRigidbody2D.velocity.x), 1);
playerRigidbody2D.AddForce(new Vector2(movementInput * movementForce * movementForceMultiplier, 0));
playerRigidbody2D.velocity = new Vector2(Mathf.Clamp(playerRigidbody2D.velocity.x, -maxHorizontalSpeed, maxHorizontalSpeed), Mathf.Clamp(playerRigidbody2D.velocity.y, -maxVerticalSpeed, maxVerticalSpeed));
if (Input.GetKeyDown(KeyCode.Space))
{
if (isTouchingGround)
{
//Play Jump sound
if (!poweredUp)
audioSource.PlayOneShot(smallJumpSound);
else
audioSource.PlayOneShot(bigJumpSound);
isJumping = true;
playerRigidbody2D.velocity = new Vector2(playerRigidbody2D.velocity.x, jumpVelocity);
jumpTimeCounter = jumpTime;
}
}
if (jumpTimeCounter > 0 && isJumping)
if (Input.GetKey(KeyCode.Space))
{
jumpTimeCounter -= Time.deltaTime;
{
playerRigidbody2D.velocity = new Vector2(playerRigidbody2D.velocity.x, jumpVelocity);
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
jumpTimeCounter = 0;
}
playerAnimator.SetFloat("movementSpeed", Mathf.Abs(playerRigidbody2D.velocity.x));
playerAnimator.SetBool("touchingGround", isTouchingGround);
}
if (movementInput > 0 && !isFacingRight)
{
FlipSprite();
}
else if (movementInput < 0 && isFacingRight)
{
FlipSprite();
}
}
void FlipSprite()
{
isFacingRight = !isFacingRight;
//Vector3 tempScale = transform.localScale;
//tempScale.x *= -1;
//transform.localScale = tempScale;
transform.Rotate(0f, 180f, 0f);
}
}
Flip method:
void FlipSprite()
{
isFacingRight = !isFacingRight;
//Vector3 tempScale = transform.localScale;
//tempScale.x *= -1;
//transform.localScale = tempScale;
transform.Rotate(0f, 180f, 0f);
}
I have problem with getting two arrow key at same time, for example Up+Right for my player to move to the corner of screen, but I can have only one input at time.
So I have to check every possibility. Is there any way around?
here is my code.
void Movement()
{
// Input user, arrow keys for move left,right,up and down/////////////////////////////
if (Input.GetKey(KeyCode.LeftArrow) && transform.position.x >= -moveRangeX) //
physicPlayer.velocity = Vector2.left * moveSpeed; //
else if (Input.GetKey(KeyCode.RightArrow) && transform.position.x <= moveRangeX) //
physicPlayer.velocity = Vector2.right * moveSpeed; //
else if (Input.GetKey(KeyCode.UpArrow) && transform.position.y <= moveRangeY) //
physicPlayer.velocity = Vector2.up * moveSpeed; //
else if (Input.GetKey(KeyCode.DownArrow) && transform.position.y >= -moveRangeY) //
physicPlayer.velocity = Vector2.down * moveSpeed; //
else //
physicPlayer.velocity = Vector2.zero; //
//////////////////////////////////////////////////////////////////////////////////////
// For multiplay input user for example when user press Up+Right arrow keys.//////////////////////////////////////////////////////////////////////////////////
if (Input.GetKey(KeyCode.RightArrow) && Input.GetKey(KeyCode.UpArrow) && transform.position.x <= moveRangeX && transform.position.y <= moveRangeY) //
physicPlayer.velocity = new Vector2(1, 1) * moveSpeed; //
else if (Input.GetKey(KeyCode.RightArrow) && Input.GetKey(KeyCode.DownArrow) && transform.position.x <= moveRangeX && transform.position.y >= -moveRangeY) //
physicPlayer.velocity = new Vector2(1, -1) * moveSpeed; //
else if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.DownArrow) && transform.position.x >= -moveRangeX && transform.position.y >= -moveRangeY) //
physicPlayer.velocity = new Vector2(-1, -1) * moveSpeed; //
else if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.UpArrow) && transform.position.x >= -moveRangeX && transform.position.y <= moveRangeY) //
physicPlayer.velocity = new Vector2(-1, 1) * moveSpeed; //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
In your case you should remove the else if statement and put it on if statement only so your code must follow something like this :
Vector3 totalMovement = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
totalMovement += transform.forward;
}
if (Input.GetKey(KeyCode.A))
{
totalMovement -= transform.right;
}
// To ensure same speed on the diagonal, we ensure its magnitude here instead of earlier
player.MovePosition(transform.position + totalMovement.normalized * speed * Time.deltaTime);
So in your code :
if(Input.GetKey(KeyCode.RightArrow)){
physicsPlayer.velocity = new Vector2(1,1) * moveSpeed;
}
if(Input.GetKey(KeyCode.UpArrow)){
physicsPlayer.velocity = new Vector2(-1,1) * moveSpeed;
}
Something like that above.
Ps: It's untested but the logic is there . Hope it helps you :)
Here is my final edit for this code. I must use physicPlayer.velocity = new Vector2(-moveSpeed, physicPlayer.velocity.y); insted of this code physicPlayer.velocity = Vector2.left * moveSpeed;
It still got some problems but it works.
Thank you.
void Update()
{
Movement();
}
void Movement()
{
physicPlayer.velocity = Vector2.zero;
if (Input.GetKey(KeyCode.LeftArrow) && transform.position.x >= -moveRangeX)
physicPlayer.velocity = new Vector2(-moveSpeed, physicPlayer.velocity.y);
if (Input.GetKey(KeyCode.RightArrow) && transform.position.x <= moveRangeX)
physicPlayer.velocity = new Vector2(moveSpeed, physicPlayer.velocity.y);
if (Input.GetKey(KeyCode.UpArrow) && transform.position.y <= moveRangeY)
physicPlayer.velocity = new Vector2(physicPlayer.velocity.x, moveSpeed);
if (Input.GetKey(KeyCode.DownArrow) && transform.position.y >= -moveRangeY)
physicPlayer.velocity = new Vector2(physicPlayer.velocity.x, -moveSpeed);
}
Hi I am using uNet Unity Multiplayer and my player controls dose not work properly. i connect 2 devices on 1 device it work good but on other device it does not move left or right nor jump. only local player can move local character
Here is the Code
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Player1 : NetworkBehaviour
{
private static Player1 playerInstance;
public static Player1 PlayerInstance {
get {
return playerInstance;
}
}
void Awake ()
{
playerInstance = this;
}
public GameObject horseRun;
public GameObject horsesaddleregdoll;
public GameObject pauseCamera;
CharacterController controller;
public Animator anim;
public int horseNum;
public GameObject cam;
void Start ()
{
if (isLocalPlayer) {
controller = GetComponent<CharacterController> ();
transform.position = position2.position;
currentPos = 2;
anim.SetInteger ("horse", 0);
AudioManagerScript.AudioInstence.horseWhinny (horseNum);
} else {
Camera cam = GetComponentInChildren <Camera> ();
cam.enabled = false;
// cam.gameObject.SetActive (false);
// this.enabled = false;
}
Time.timeScale = 0;
position1 = GameObject.Find ("Position1").transform;
position2 = GameObject.Find ("Position2").transform;
position3 = GameObject.Find ("Position3").transform;
position4 = GameObject.Find ("Position4").transform;
}
public float speed = 10.0f;
public Transform position1;
public Transform position2;
public Transform position3;
public Transform position4;
int currentPos = 1;
bool moveFromPos1ToPos2;
bool moveFromPos2ToPos1;
bool moveFromPos2ToPos3;
bool moveFromPos3ToPos2;
bool moveFromPos3ToPos4;
bool moveFromPos4ToPos3;
float timeMove = 0;
public float jumpspeed = 10.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
public Vector2 findDirection;
public Vector2 startPoint;
public Vector2 endPoint;
// Update is called once per frame
void Update ()
{
if (isLocalPlayer) {
timerStep -= Time.deltaTime;
gameObject.transform.position += new Vector3 (0, 0, Time.deltaTime * 80.0f);
if (moveFromPos1ToPos2) {
if (timeMove >= .5f) {
transform.position = new Vector3 (position1.position.x, transform.position.y, transform.position.z);
transform.localEulerAngles = new Vector3 (0, 0, 0);
moveFromPos1ToPos2 = false;
currentPos = 2;
Debug.Log ("Current Pos" + currentPos);
}
timeMove += Time.deltaTime;
transform.position = Vector3.Lerp (new Vector3 (position1.position.x, transform.position.y, transform.position.z), new Vector3 (position2.position.x, transform.position.y, transform.position.z), timeMove * 2);
}
if (moveFromPos2ToPos1) {
if (timeMove >= .5f) {
transform.position = new Vector3 (position2.position.x, transform.position.y, transform.position.z);
transform.localEulerAngles = new Vector3 (0, 0, 0);
moveFromPos2ToPos1 = false;
currentPos = 1;
Debug.Log ("Current Pos" + currentPos);
}
timeMove += Time.deltaTime;
transform.position = Vector3.Lerp (new Vector3 (position2.position.x, transform.position.y, transform.position.z), new Vector3 (position1.position.x, transform.position.y, transform.position.z), timeMove * 2);
}
if (moveFromPos2ToPos3) {
if (timeMove >= .5f) {
transform.position = new Vector3 (position2.position.x, transform.position.y, transform.position.z);
transform.localEulerAngles = new Vector3 (0, 0, 0);
moveFromPos2ToPos3 = false;
currentPos = 3;
Debug.Log ("Current Pos" + currentPos);
}
timeMove += Time.deltaTime;
transform.position = Vector3.Lerp (new Vector3 (position2.position.x, transform.position.y, transform.position.z), new Vector3 (position3.position.x, transform.position.y, transform.position.z), timeMove * 2);
}
if (moveFromPos3ToPos2) {
if (timeMove >= .5f) {
transform.position = new Vector3 (position3.position.x, transform.position.y, transform.position.z);
transform.localEulerAngles = new Vector3 (0, 0, 0);
moveFromPos3ToPos2 = false;
currentPos = 2;
Debug.Log ("Current Pos" + currentPos);
}
timeMove += Time.deltaTime;
transform.position = Vector3.Lerp (new Vector3 (position3.position.x, transform.position.y, transform.position.z), new Vector3 (position2.position.x, transform.position.y, transform.position.z), timeMove * 2);
}
if (moveFromPos3ToPos4) {
if (timeMove >= .5f) {
transform.position = new Vector3 (position3.position.x, transform.position.y, transform.position.z);
transform.localEulerAngles = new Vector3 (0, 0, 0);
moveFromPos3ToPos4 = false;
currentPos = 4;
Debug.Log ("Current Pos" + currentPos);
}
timeMove += Time.deltaTime;
transform.position = Vector3.Lerp (new Vector3 (position3.position.x, transform.position.y, transform.position.z), new Vector3 (position4.position.x, transform.position.y, transform.position.z), timeMove * 2);
}
if (moveFromPos4ToPos3) {
if (timeMove >= .5f) {
transform.position = new Vector3 (position4.position.x, transform.position.y, transform.position.z);
transform.localEulerAngles = new Vector3 (0, 0, 0);
moveFromPos4ToPos3 = false;
currentPos = 3;
Debug.Log ("Current Pos" + currentPos);
}
timeMove += Time.deltaTime;
transform.position = Vector3.Lerp (new Vector3 (position4.position.x, transform.position.y, transform.position.z), new Vector3 (position3.position.x, transform.position.y, transform.position.z), timeMove * 2);
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
}
}
//.................................. Horse Run to Jump & Jump to Run Animation States ......................
public void HorseJumpState ()
{
Debug.Log ("JUMP HORSE" + gameObject.name);
moveDirection.y = jumpspeed;
controller.radius *= 0.5f;
Invoke ("JumpTime", .8f);
Invoke ("HorseRunState", 0.75f);
}
public void HorseRunState ()
{
anim.SetInteger ("horse", 0);
}
public void HorseIdleState ()
{
anim.SetInteger ("horse", 5);
}
float timerStep = 1.25f;
void JumpTime ()
{
controller.radius *= 2;
}
public void JumpButton ()
{
if (isLocalPlayer) {
Time.timeScale = 1;
Debug.Log ("timer" + timerStep);
if (timerStep <= 0 && controller.isGrounded) {
timerStep = 1.25f;
{
anim.SetInteger ("horse", 1);
Invoke ("HorseJumpState", .00001f);
AudioManagerScript.AudioInstence.jumpSound (horseNum);
}
}
}
}
public void SwipeLeft ()
{
if (position1 == null || position2 == null || position3 == null || position4 == null) {
position1 = GameObject.Find ("Position1").transform;
position2 = GameObject.Find ("Position2").transform;
position3 = GameObject.Find ("Position3").transform;
position4 = GameObject.Find ("Position4").transform;
}
Time.timeScale = 1;
if (isLocalPlayer) {
if (currentPos == 2 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
moveFromPos2ToPos1 = true;
transform.localEulerAngles = new Vector3 (0, -15, 0);
timeMove = 0;
} else if (currentPos == 3 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
moveFromPos3ToPos2 = true;
transform.localEulerAngles = new Vector3 (0, -15, 0);
timeMove = 0;
} else if (currentPos == 4 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
moveFromPos4ToPos3 = true;
transform.localEulerAngles = new Vector3 (0, -15, 0);
timeMove = 0;
}
}
}
public void SwipeRight ()
{
if (position1 == null || position2 == null || position3 == null || position4 == null) {
position1 = GameObject.Find ("Position1").transform;
position2 = GameObject.Find ("Position2").transform;
position3 = GameObject.Find ("Position3").transform;
position4 = GameObject.Find ("Position4").transform;
}
Time.timeScale = 1;
if (isLocalPlayer) {
if (currentPos == 1 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
moveFromPos1ToPos2 = true;
transform.localEulerAngles = new Vector3 (0, 15, 0);
timeMove = 0;
} else if (currentPos == 2 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
moveFromPos2ToPos3 = true;
transform.localEulerAngles = new Vector3 (0, 15, 0);
timeMove = 0;
} else if (currentPos == 3 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
moveFromPos3ToPos4 = true;
transform.localEulerAngles = new Vector3 (0, 15, 0);
timeMove = 0;
}
}
}
bool gameOver = false;
void GameOveronTriggerEnter ()
{
if (!gameOver) {
gameOver = true;
GUIManager.GuiInstance.gameOver.SetActive (true);
GUIManager.GuiInstance.GrassObstaclesDestroy ();
GUIManager.GuiInstance.Invoke ("AdsCall", .7f);
AudioManagerScript.AudioInstence.pauseSound ();
GUIManager.GuiInstance.scorecheck ();
}
}
void DisableScript ()
{
GameObject player = GameObject.FindGameObjectWithTag ("Player");
player.GetComponent<Player1> ().enabled = false;
GameObject[] anims = GameObject.FindGameObjectsWithTag ("animator");
for (int i = 0; i < anims.Length; i++)
anims [i].GetComponent<Animator> ().enabled = false;
}
bool gameOverfalse = false;
//.................... Obstacles Triggers .................
void OnTriggerEnter (Collider coll)
{
int grass = GUIManager.GuiInstance.grass;
if (coll.gameObject.tag == "hurdle" || coll.gameObject.tag == "bridgehurdles" && gameOverfalse == false) {
gameOverfalse = true;
horseRun.SetActive (false);
horsesaddleregdoll.SetActive (true);
AudioManagerScript.AudioInstence.horseDie (horseNum);
gameObject.GetComponent<Player1> ().enabled = false;
pauseCamera.SetActive (false);
Invoke ("GameOveronTriggerEnter", 2.0f);
Invoke ("DisableScript", 1.5f);
GUIManager.GuiInstance.gamePanelSingle.SetActive (false);
GUIManager.GuiInstance.singleWater = false;
} else if (coll.gameObject.tag == "Single") {
GUIManager.GuiInstance.score += 10 * grass;
} else if (coll.gameObject.tag == "Double") {
GUIManager.GuiInstance.score += 25 * grass;
} else if (coll.gameObject.tag == "Tripple") {
GUIManager.GuiInstance.score += 50 * grass;
} else if (coll.gameObject.tag == "Forth") {
GUIManager.GuiInstance.score += 100 * grass;
} else if (coll.gameObject.tag == "Grass") {
Debug.Log ("Grass", coll.gameObject);
GUIManager.GuiInstance.grass++;
AudioManagerScript.AudioInstence.Collectable (horseNum);
//GUIManager.GuiInstance.score+= 250*2;
Destroy (coll.gameObject);
} else if (coll.gameObject.tag == "grassreset") {
Debug.Log ("Grass", coll.gameObject);
GUIManager.GuiInstance.grass = 1;
//GUIManager.GuiInstance.score+= 250*2;
Destroy (coll.gameObject);
}
if (coll.gameObject.tag == "snow") {
Debug.Log ("Trigger Enter");
GUIManager.GuiInstance.snow.SetActive (true);
}
}
bool dontMove = false;
//..................While Horse on Bridge ......................
void OnTriggerStay (Collider colstay)
{
if (colstay.gameObject.tag == "Bridge") {
dontMove = true;
Debug.Log ("Trigger Stay");
moveFromPos1ToPos2 = false;
moveFromPos2ToPos1 = false;
transform.localEulerAngles = new Vector3 (0, 0, 0);
// cam.GetComponent<SmoothFollow> ().height = 50;
}
}
void OnTriggerExit (Collider colexit)
{
if (colexit.gameObject.tag == "Bridge") {
dontMove = false;
// cam.GetComponent<SmoothFollow> ().height = 35;
}
if (colexit.gameObject.tag == "snow") {
Debug.Log ("Trigger Exit");
GUIManager.GuiInstance.snow.SetActive (false);
}
}
}
In UNET, Before performing any operation into non-player object you have to first acquire its authority. After getting the authority, you will become able to perform an action that will sync across network.
For details You can check my this answer.