Hi,
My first question here - thanks in advance for your help.
I have a PlayerScript where sometimes the player (a cat character) is sitting, and so a bool in that script is then set to true.
I’d like to get this bool and use it in the script that is attached to my camera - so that the camera moves to LookAt a different spot (an empty child of the player that is ahead of the player) rather than looking at the player’s transform which should happen when sitting is set to false. I’ve read some answers and material but feel I must be missing something as the camera script is not finding the sitting value from the PlayerScript. Any help is appreciated.
Again - the PlayerScript contains the bool “sitting” and this works fine within that script .
Here is the separate script attached to the camera:
using System;
using UnityEngine;
namespace UnityStandardAssets.Cameras
{
public class LookatTarget : MonoBehaviour {
private Transform playerTransform;
public GameObject catPlayer;
private PlayerScript playerScript;
void Start () {
playerScript = catPlayer.GetComponent<PlayerScript>();
}
void Update () {
if (playerScript.sitting == true) {
playerTransform = GameObject.Find("sitSpot").transform;
} else {
playerTransform = GameObject.FindWithTag ("Player").transform;
}
transform.LookAt (playerTransform);
}
}
}
And here is the error:
Assets/Cameras/Scripts/LookatTarget.cs(18,25): error CS0246: The type or namespace name `PlayerScript’ could not be found. Are you missing a using directive or an assembly reference?
EDIT: Here is the PlayerScript. I stripped out some collision stuff that I think is irrelevant to my problem here in order to try to make it easier to follow.
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (CapsuleCollider))]
public class PlayerScript : MonoBehaviour {
private Animator anim;
private int animpar;
public float moveSpeed = 6.0f;
private Vector3 moveDirection = Vector3.zero; // Direction we're moving in.
private Vector3 targetDirection = Vector3.zero; // Direction we want to move in.
private float rotateSpeed = 5; // Speed to rotate at.
private Vector3 right = Vector3.zero;
private Vector3 forward = Vector3.zero;
public Vector3 MoveVector{ set; get;}
public Vector3 cameraLevelVec;
public Vector3 camActualRot;
public bool canJump = true;
public bool sitting = false;
private bool touching = false;
private bool nottouching = true;
public float gravity = 20.0f;
public float jumpHeight = 2.0f;
private bool grounded = false;
private float camAngleZ;
private float smoothing;
public Rigidbody rb;
public Camera MainCamera;
private Transform lawnCameraSpotTransform;
private Transform pressCameraSpotTransform;
private Transform camSitSpotTransform;
private Transform catFollowCamSpotTransform;
private Transform cameraActualTransform;
//active camera
private Transform cameraTransform;
public bool freezeRotation;
float horizontal = 0.0f;
float vertical = 0.0f;
bool directionChosen = false;
bool newRoomTriggered = false;
private Vector2 touchOrigin = -Vector2.one;
private Vector2 touchDirection;
public BoxCollider lawnCollider;
public BoxCollider pressCollider;
private bool lawnCollided = true;
private bool pressCollided = false;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
anim = gameObject.GetComponentInChildren<Animator> ();
MainCamera = GameObject.Find("MainCamera").GetComponent<Camera>();
lawnCameraSpotTransform = GameObject.Find("lawnCameraSpot").transform;
pressCameraSpotTransform = GameObject.Find("pressCameraSpot").transform;
camSitSpotTransform = GameObject.Find("camSitSpot").transform;
cameraActualTransform = GameObject.Find("MainCamera").transform;
//camera starting spot for movement/direction
cameraTransform = lawnCameraSpotTransform;
//move camera itself to start spot
Vector3 cameraPos = cameraTransform.position;
MainCamera.transform.position = cameraPos;
GetComponent<Rigidbody>().freezeRotation = true;
GetComponent<Rigidbody>().useGravity = false;
}
// Update is called once per frame
void Update () {
catFollowCamSpotTransform = GameObject.Find("catFollowCamSpot").transform;
if (Input.touchCount > 0) {
Touch touch = Input.touches [0];
if (touch.phase == TouchPhase.Began) {
touchOrigin = touch.position;
// Determine direction by comparing the current touch position with the initial one.
} else if (touch.phase == TouchPhase.Moved) {
touchDirection = touch.position - touchOrigin;
horizontal = touch.position.x - touchOrigin.x;
vertical = touch.position.y - touchOrigin.y;
touching = true;
} else if (touch.phase == TouchPhase.Ended) {
touching = false;
horizontal = 0.0f;
vertical = 0.0f;
touchOrigin.x = -1;
touchOrigin.y = -1;
}
}
MoveVector = CameraFixed();
Move ();
SwitchCameraPosition ();
//Animation
if (grounded) {
//if (CrossPlatformInputManager.GetAxis ("Vertical") != 0) {
if (vertical != 0) {
anim.SetInteger ("AnimPar", 1);
sitting = false;
canJump = true;
smoothing = 0.1f;
} else {
anim.SetInteger ("AnimPar", 0);
//canJump = false;
}
//Sit
//if (CrossPlatformInputManager.GetButton ("Sit")) {
if (Input.touchCount == 3) {
anim.SetInteger ("AnimPar", 2);
sitting = true;
cameraTransform = camSitSpotTransform;
smoothing = 0.2f;
}
// Jump
//if (canJump && CrossPlatformInputManager.GetButton ("Jump")) {
if (canJump && (Input.touchCount == 2)) {
//&& (sitting == false)
anim.SetInteger ("AnimPar", 4);
rb.AddForce (Vector3.up * jumpHeight, ForceMode.Impulse);
}
// We apply gravity manually for more tuning control
rb.AddForce(new Vector3 (0, -gravity * rb.mass, 0));
grounded = false;
}
private Vector3 CameraFixed() {
Vector3 dir = Vector3.zero;
dir.x = horizontal;
dir.z = vertical;
//get the forward-facing direction of the camera
Vector3 cameraForward = cameraActualTransform.TransformDirection (Vector3.forward);
cameraForward.y = 0; //set to 0 because of camera rotation on the X axis
//get the right-facing direction of the camera
Vector3 cameraRight = cameraActualTransform.TransformDirection (Vector3.right);
if (dir.magnitude > 1)
dir.Normalize ();
Vector3 refDir = dir.x * cameraRight + dir.z * cameraForward;
//return refDir.normalized * MoveVector.magnitude;
return refDir;
}
private void Move() {
if (sitting == false) {
rb.MovePosition (rb.position + MoveVector * moveSpeed * Time.deltaTime);
//rotate cat in direction of movement
if ((horizontal != 0) && (vertical != 0)) {
transform.rotation = Quaternion.LookRotation(MoveVector);
}
}
}
void OnCollisionStay (Collision other) {
if (other.gameObject.tag == "Ground") {
grounded = true;
}
}
public void SwitchCameraPosition () {
//if (touching == false) {
if (sitting == false) {
if (pressCollided == true) {
cameraTransform = pressCameraSpotTransform;
}
}
MainCamera.transform.position = Vector3.Lerp (cameraActualTransform.position, cameraTransform.position, smoothing);
}
}
Thanks!!
system
2
@haldanemcfall, I’m not a pro in any way in C# namespaces, but logic tells me that:
You need to try to make PlayerScript visible for LookatTarget. You may either
- remove namespace UnityStandardAssets.Cameras from LookatTarget file, or
- remove some namespace definition from PlayerScript file. So they both will be in “default” namespace.
- Or use ‘using’ instruction for PlayerScript namespace at the top of LookatTarget file the same way You do with System and UnityEngine.
Laiken
3
If it’s a single player game and there’s only a PlayerScript in the scene, try
void Start () {
playerScript = FindObjectOfType(typeof(PlayerScript)) as PlayerScript;
}
If there’s only one PlayerScript in the scene, you can also make the sitting bool be static and make
if (PlayerScript.sitting == true) // note the uppercase "P"
But before that, just in case, make sure that you dragged the GameObject catPlayer to the catPlayer slot in the camera script (on the inspector).
Also, again, just in case, make sure that you put the PlayerScript on the catPlayer directly (and not a child of catPlayer)